1/21/2010

SharePoint: List all SPBasePermissions for a User

 

This is a sample of code to list every possible permission a user has for a particular object. The object in the code is an SPWeb, but could be a list, folder or item. As I wanted to iterate though the SPBasePermissions enumberation and display the permission names I used the code sample from here.

For a list of the SharePoint permissions see here: http://techtrainingnotes.blogspot.com/2010/01/sharepoint-permission-levels.html

 

There’s got to be a better way to do this… but it works.

 

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //username = SPContext.Current.Web.CurrentUser.LoginName
            string username = @"maxsp2007\stellas";
           
            using (SPSite site = new SPSite("http://MAXsp2007/sites/training"))
            {
                SPWeb web = site.OpenWeb("ateamsite") ; //.RootWeb;
 
                foreach (string enumName in Enum.GetNames(typeof(SPBasePermissions)))
                {
                    if (web.DoesUserHavePermissions(username,
                        (SPBasePermissions)Enum.Parse(typeof(SPBasePermissions), enumName)))
                    {
                        Console.WriteLine(enumName);
                    }
                }
 
                Console.ReadLine();
            }
        }
    }
}

 

The result:

image

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.