Sunday, January 18, 2009

How to insert a sharepoint user in c#

We can insert active directory users using follwing code segment



public void AddUserToSP()
{
SPWeb spWeb = null;

try
{
//Open the SharePoint site
spWeb =site.AllWebs[(ConfigurationManager.AppSettings["WEB_SITE"].ToString())];

//Assign role and add user to site
//SPRoleAssignment spRoleAssignment =new SPRoleAssignment(details[0],details[1],details[2],details[3]);
string strInFormAuthenticaton = "Specify your LoginName Here";
strInFormAuthenticaton = "ad:"+strInFormAuthenticaton;

spWeb.SiteUsers.Add(strInFormAuthenticaton,"Specify your Name Here","Specify your Email Here","Specify your Notes Here");

//SPRoleAssignment spRoleAssignment = new SPRoleAssignment(strInFormAuthenticaton, details[1], details[2], details[3]);
////Using Contribute, might need high access
//SPRoleDefinition roleDefinition = spWeb.RoleDefinitions["Read"];
//spRoleAssignment.RoleDefinitionBindings.Add(roleDefinition);
//spWeb.AllowUnsafeUpdates = true;
//spWeb.RoleAssignments.Add(spRoleAssignment);
//spWeb.Update();

//Update site
//spWeb.Update();
}
catch (Exception)
{
}
finally
{
spWeb.Close();
}
}

No comments: