My Apps on the SharePoint Store

My Books

  • Apps
  • Azure
  • REST
  • CSOM/JSOM
  • Service Apps
  • WCF
  • REST/OData
  • CSOM/JSOM
  • SharePoint API
  • Silverlight
  • jQuery

Recent blog posts

User login

Home | Blogs | Stephane Eyskens's blog

Leveraging .NET 4 in SharePoint 2013 Series (1) - Tuples

Hi,

I'll be writing a series of blog posts about leveraging .NET 4 within SharePoint 2013. As you might know already, SharePoint 2013 is now based on this .NET runtime version v4.0.30319 which actually allows developers to benefit from .NET 4 features. You couldn't do that with SharePoint 2010 which was still based on .NET runtime v2.0.50727.

Tuples

Say you want to easily (because you're lazy) create a method that returns all the users of a given site with their associated roles (visitor, contributor, owner). Without having to create a custom class, struct, you can directly make use of the .NET 4 Tuple Class to do the job in only a few lines of code:

static void Main(string[] args)
{
    using(SPSite Site = new SPSite("http://sp2013"))
    {
        using(SPWeb Web=Site.OpenWeb())
        {
            List<Tuple<string, bool, bool, bool>> UserRoleItems = GetUserRolesForWeb(Web);
            foreach (var UserRoleItem in UserRoleItems)
            {
                Console.WriteLine(
                    "User {0}, Read Permission : {1}, Write Permission : {2}, Owner : {3}",
                    UserRoleItem.Item1, UserRoleItem.Item2, UserRoleItem.Item3, UserRoleItem.Item4);
            }
        }
            
    }
    Console.Read();
}
private static List<Tuple<string, bool, bool, bool>> GetUserRolesForWeb(SPWeb web) {
            
    List<Tuple<string, bool, bool, bool>> ReturnedUserRoles = new List<Tuple<string, bool, bool, bool>>();
    foreach (SPUser User in web.AllUsers)
    {
        ReturnedUserRoles.Add(
            Tuple.Create(User.ID.ToString(),
            web.DoesUserHavePermissions(SPBasePermissions.ViewListItems),
            web.DoesUserHavePermissions(SPBasePermissions.EditListItems),
            web.DoesUserHavePermissions(SPBasePermissions.ManageWeb)));
    }
    return ReturnedUserRoles;
            
}

which will return :

Happy Coding!

Comments

Hello stephane, I think there

Hello stephane,

I think there is a mistake in you code

" web.DoesUserHavePermissions(SPBasePermissions.ViewListItems),
web.DoesUserHavePermissions(SPBasePermissions.ViewListItems),
web.DoesUserHavePermissions(SPBasePermissions.ViewListItems)));"

you add 3 times the view list item (it's not the point for this article but it could be disturbing) ;)

Best regards

Christopher.

Mistake in the code

Hehe,

You're right, I meant Contribute & Owner. Fixed :).

Thanks

SharePoint 2010 code in ASP.NET 4.0

Hi,
Can you deploy wsp solution written for SP 2010 into SharePoint 2013 assuming all api's are still valid. Is there any reason to recompile in .NET 4.0 if you don't plan to make changes to code to leverage anything new?

Thanks

Recompiling

Hi,

It's indeed not a must! It was the same between 2007 & 2010, you weren't forced to recompile, you could just deploy 2007 packages over 2010. However, depending on what you've written, you might find out some discrepencies or if you want to leverage 2013 specific functionalities, then, you'll have to rebuild your package of course.

Best Regards