Saturday, May 24, 2008

Check User Permissions in Windows SharePoint Services or MOSS 2007

Windows SharePoint Services 3.0 implements new types in its object model to support the new security model. Old model is obsolete but keep beeing usable. In Windows SharePoint Services 3.0 or MOSS 2007 sites, you have sometimes to check user, Permissions, Rights, Roles, when loading a page, doing an action, and decide what to do depending on these Permissions. all these Classes are obosolete :
  • SPPermission
  • SPRole
  • SPRights
There is however several Methods working around these tasks, for example :
  • SPSite.CheckPermissions
  • SPSite.DoesUserHavePermissions
  • SPWeb.CheckPermissions
  • SPWeb.DoesUserHavePermissions
  • SPList.CheckPermissions
  • SPList.DoesUserHavePermissions
  • SPListItem.CheckPermissions
  • SPListItem.DoesUserHavePermissions
  • ...
Here is a sample of C# code to check user specific Permission to a web site (SPWeb). For instance, we check if user can edit list items.
Code Sample :

        SPWeb myWeb = SPContext.Current.Site.OpenWeb();


        if (myWeb.DoesUserHavePermissions("DomainName\\user1", SPBasePermissions.EditListItems))


            System.Diagnostics.Debug.WriteLine("user1 can edit lisItems");




            System.Diagnostics.Debug.WriteLine("user1 cannot edit lisItems");


if you are puzzled by "System.Diagnostics.Debug.." and want to know more about DebugView, see...
Use DebugView in Windows SharePoint Services 3.0 programming.


itypo said...

Our company developed a small web part for determining effective permissions for the users around SharePoint sites. You can read more about it at or download it free from our company site

Hope it will be helpful for most of you!

Khushi said...

Can we findout the permission on a web application using code? Every blog mentions the permission check from site collection to item level. I want whether user has permission on a SPWebapplication.

Rumah Dijual said...

It has taken me ages to discover your site. Finally. This is just the information I was looking for.

Sri Varshan said...

Thanks for the nice article, Have a look at the article "Check User Permissions Programmatically in SharePoint 2010"