Tuesday, March 2, 2010

Custom web service code to find out whether a user belongs to perticular group

Add Following code in web service and publish this web service, it has two parameters that needs to be passed

1) employee login : should be in the form DomainName\username

2) Group name : which is group name you have created in sharepoint site.



Add web reference to http:///_vti_bin/usergroup.asmx.

Also put your server name where I have indicated below



[WebMethod]

public String IsMOSSUserGroup(String employeeUserName, String group)

{

try

{

ServerName.UserGroup objWebService = new ServerName.UserGroup();

objWebService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndGroups = objWebService.GetGroupCollectionFromUser(employeeUserName);

Boolean blnFound = false;

String strGroup = group;

foreach (System.Xml.XmlNode xmlGroup in ndGroups.ChildNodes[0].ChildNodes)

{

if (strGroup == xmlGroup.Attributes["Name"].Value)

{

blnFound = true;

}

}

if (blnFound == true)

{

return "Yes";

}

else

{

return "No";

}

}

catch (Exception ex)

{

return ex.Message;

}

}

This web service will return Yes if the given user belongs to the given sharepoint group.

No comments:

Post a Comment