web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Adding User to SharePoint Group through a custom ASP.NET web service.

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

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
{
s
tring 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.

Comments

*This post is locked for comments