Sunday, October 5, 2008

Checking user permission for lists

Scenario:
You want to check user permission on all the lists
Solution:
You can develop a webpart and using object model you can check that.

Code:

StringBuilder sb = new StringBuilder();
sb.Append("<UL>");

SPSite sitecol = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
SPUser user = SPContext.Current.Web.CurrentUser;

SPSecurity.RunWithElevatedPrivileges( delegate() {
using (SPSite elevatedSiteCol = new SPSite(sitecol.ID)) {
using (SPWeb elevatedSite = elevatedSiteCol.OpenWeb(site.ID)) {
foreach (SPList list in elevatedSite.Lists) {
if (list.DoesUserHavePermissions(user,SPBasePermissions.ViewListItems))
{
sb.Append("<LI><a href='" + list.RootFolder.Url + "'>" + list.Title + "</a></LI>");
}
else
{
sb.Append("<LI style='color:red'>" + list.Title + "</LI>");
}
}
}
}
}
);

sb.Append("</UL>");
Note:
http://daniellarson.spaces.live.com/blog/cns!D3543C5837291E93!2005.entry?wa=wsignin1.0&sa=201418415

0 comments: