Wednesday, October 17, 2012

How to retrieve users permission using Sharepoint Object model code



SAMPLE CODE 

static void Main()
{
try
{
using (SPSite oSiteCollection = new SPSite("http://SiteURL/"))  
{
SPWebCollection collWebsites = oSiteCollection.AllWebs;
Console.WriteLine("Websites Count: {0}", collWebsites.Count);
Console.WriteLine(" ");
string rootwebtitle = oSiteCollection.RootWeb.Title;
foreach (SPWeb oWebsite in collWebsites)
{
Console.WriteLine("Site : " + oWebsite.Title);

string websiteurl = oWebsite.Url;
Console.WriteLine("Website Url : " + websiteurl);
allUser(websiteurl);
oWebsite.Dispose();
Console.WriteLine("--------------------");
}
}

Console.WriteLine("------Program Completed Please click enter to exit------");  
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error occured" + ex.Message);
Console.WriteLine("------Program terminated due to error. Please click enter to exit------");
Console.ReadLine();
}


}


public static void allUser(string url)
{


using (SPSite site = new SPSite(url))
{

using (SPWeb web = site.OpenWeb())
{
foreach (SPUser user in web.AllUsers)  

{


if (user.Name != "NT AUTHORITY\\LOCAL SERVICE" && user.Name != "NT AUTHORITY\\Authenticated Users")
{
Console.WriteLine(user.Name);
SPRoleAssignment assignment = web.RoleAssignments.GetAssignmentByPrincipal(user);

Console.WriteLine("RoleAssignment Name :- " + assignment.Member + " :::: RoleAssignment Parent :- " + assignment.Parent);
SPRoleDefinitionBindingCollection roleDefinitions = assignment.RoleDefinitionBindings;

foreach (SPRoleDefinition roleDef in roleDefinitions)
{
Console.WriteLine("RoleDefinition Name :- " + roleDef.Name + " Base Permission :- " + roleDef.BasePermissions);
Console.WriteLine("________________________________________________________________________");
}
}

}

}

}
}

How to use Scope Id in Sharepoint object model

If the lists items inherits permission or doesn’t inherit permission from the parent list or site, then the list items(including Sub folders) will have a same “ScopeID”

Based on the ACL i.e permissions, the scopeID displays a number

For Full Control permission, the below program returns the number 15.  
 
For View Only permission, the below program returns the number 1.  

   

Sample code:-


using (SPSite site = new SPSite("SiteURL"))
{
using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists["List_Name"];
Console.WriteLine("List name = {0}", list.Title);


SPQuery query = new SPQuery();
query.ViewFields = "";
query.ViewAttributes = "Scope='RecursiveAll'";

SPListItemCollection items = list.GetItems(query);

foreach (SPListItem item in items)
{
Console.WriteLine("{0} --> {1}",item["Title"],item["ScopeId"]);
}

Console.ReadKey();
}
}

Tuesday, October 16, 2012

How To Create an ECB menu to Wiki library in Sharepoint 2010


In order to add a custom menu to any list/library in sharepoint, we need to create a custom action; We need to specify the registrationID (templateID) correctly to identify the list template. List of template IDs can be found here

Steps

1.     Create a new Empty Sharepoint project
2.     Deploy as a farm solution
3.     Add New Feature


4.     By default it will be added as Feature1; Rename the feature


        5.  Select scope to "Web"


6.     Add new item and select Empty element




7.     Add the below xml to the element.xml;
     The registration Id 119 corresponds to the wiki library.


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="CA_Wiki"
    RegistrationType="List"
    RegistrationId="119"
    ImageUrl="/_layouts/1033/Images/RTEDCELL.GIF"
    Location="EditControlBlock"
    Sequence="301"
    Title="My Custom ECB Menu Item for Wiki" >
    <UrlAction
      Url="http://msn.com"/>
   
  </CustomAction>
</Elements>

8.     Deploy the solution and check in your wiki library