Saturday, May 24, 2008

Check User Permissions in Windows SharePoint Services or MOSS 2007

Introduction:

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");


        }


        else


        {


            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.

No comments:

Post a Comment