Adding User to SharePoint Group through a custom ASP.NET web service.
I had a requirement to create a web service which would be used to add user to a SharePoint group. The code for it
[WebMethod]
public string AddSharePointUser(string username,string password, string email){
string statusInfo = āā;
try{
string SharePointUrl = ConfigurationManager.AppSettings[āSharePointUrlā].ToString();
string SharePointGroup = ConfigurationManager.AppSettings[āSharePointGroupā].ToString();
SPSecurity.RunWithElevatedPrivileges(delegate(){
using (SPSite oSiteCollection = new
SPSite(SharePointUrl)){
oSiteCollection.AllowUnsafeUpdates = true;
using (SPWeb oWebsite = oSiteCollection.OpenWeb()){
oWebsite.AllowUnsafeUpdates = true;
SPUser spUser = oWebsite.EnsureUser(username);
SPGroup spGroup = oWebsite.SiteGroups[SharePointGroup];
spGroup.AddUser(spUser);
oWebsite.AllowUnsafeUpdates = false;
}
oSiteCollection.AllowUnsafeUpdates = false;
}});
statusInfo = āUser Created Successfullyā;
}
else{
statusInfo = āUser Already Existsā;
}}
catch (Exception ex){
statusInfo = ex.Message;
}
return statusInfo;
}
To run the web service successfully, we need to set the identity of application pool of the web service to SharePoint Administrator account.
Hope it helps.
Filed under: SharePoint, SharePoint 2010 Tagged: SharePoint, SharePoint 2010
This was originally posted here.

Like
Report
*This post is locked for comments