Friday, June 6, 2008

Adding Active Directory User to Site

Hi All,

Here is a way to add active directory user at site level.

There is one method that ensures that specific loginname exist in site or not? and if not, then adds that user in the site.

Here is a qucik way to do it.

SPWeb.EnsureUser Method (Microsoft.SharePoint)

SPWeb instance ;
String loginName;
SPUser = returnValue;

returnValue = instance.EnsureUser[loginName];

This checks the loginname and if not exists then adds in site.

SPSite siteCollection = SPContext.Current.Site;

SPWeb topLevelSite = siteCollection.RootWeb();

'-------- "Ensure" user and store in a SPUser object: ---------------

SPUser spUserObj = topLevelSite.EnsureUser("domain\someUserName");

'-------- Get the SiteUserInfoList and loop through it to find the
previously "ensured" user: ---------------

SPList userInfoList = topLevelSite.SiteUserInfoList;
SPListItem =InfoItem ;

foreach(userInfoItem In userInfoList.Items)
if(userInfoItem.Item("Account") == spUserObj.LoginName)
userInfoItem.Item["Department"] = "some department value";

Then add user to specifc group.

for more information: Click Here

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to