Showing posts with label Object Model. Show all posts
Showing posts with label Object Model. Show all posts

Sunday, November 23, 2014

Sharepoint Custom Claims provider - Fill Resolve method implementation.


In order to have the fillresolve methods to be triggered, you need to have the property SupportsResolve to true.

We will have 2 overload methods of fillResolve in your custom claims provider. You need to implement both of these methods.

protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved) - This method will be called when you enter name in the text box and click resolve.

and

protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved) - This method will be called when you enter name in the people picker and click add, it will call fillresolve as it needs to resolve the name as soon as we have an entry in textbox.


You need to implement the Picker entity; Use SPClaim constructor instead of CreateClaim constructor.

private PickerEntity GetPickerEntity(string ClaimValue, string type)
{
PickerEntity pe = CreatePickerEntity();
 
/* in my case, emailaddress is the identity claim. Make sure that the last parameter TrustedProvider is correct, otherwise name resolution will not be correct. */

pe.Claim = new SPClaim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", ClaimValue, ClaimValueType, SPOriginalIssuers.Format(SPOriginalIssuerType.TrustedProvider, "Trusted User"));  

pe.EntityData[PeopleEditorEntityDataKeys.AccountName] = ClaimValue;
pe.EntityData[PeopleEditorEntityDataKeys.Email] = ClaimValue;
pe.DisplayText = ClaimValue;
pe.EntityData[PeopleEditorEntityDataKeys.DisplayName] = ClaimValue;
pe.EntityType = SPClaimEntityTypes.User;
pe.IsResolved = true;
return pe;
}

More Information

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.claims.spclaim.spclaim.aspx

http://blogs.technet.com/b/speschka/archive/2010/05/25/replacing-the-out-of-box-name-resolution-in-sharepoint-2010-part-2.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.claims.spclaimprovider.fillsearch.aspx

Sunday, March 23, 2014

Sharepoint - List all Site Level and Web Level features programmatically


The below sample is just for listing all site feature and current web level features.


static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://site collection url"))
            {
                Console.WriteLine("");
                Console.WriteLine("Site URL=" + site.Url);
                Console.WriteLine("");
                foreach (SPFeature f in site.Features)
                {
                    Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                }


                using (SPWeb web = site.OpenWeb())
                {
                    Console.WriteLine("");
                    Console.WriteLine("\n\n Web URL=" + web.Url + "\n\n");
                    Console.WriteLine("");
                    foreach (SPFeature f in web.Features)
                    {
                        Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                    }
                }
            }
            Console.WriteLine("");
            Console.WriteLine("Program Completed");

        }



If you want to display all web level features from all the subwebs, you need to write a recursive function to loop through all subsites and display the features.

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();
}
}