All About SharePoint

Liedong(Ken) Zheng,SharePoint Leader at SIMPLOT

Posts Tagged ‘SPUser’

Get SPUser object from SharePoint List Item People/Group picker field

Posted by ken zheng on January 29, 2010

The code below is to get SPUser from a multiple user item column

string strURL = "http://teams/finance/Lists/Product Cost Plant Email List/";
            using (SPSite oSPSite = new SPSite(strURL))
            {
                using (SPWeb oSPWeb = oSPSite.OpenWeb())
                {
                    
                    SPList list = oSPWeb.GetList(strURL);
                    SPListItemCollection items = list.Items;
                    foreach (SPListItem oListItem in items)
                    {
                        if (oListItem["Title"].ToString() == "Test")
                        {
                            String usersString = oListItem["Audience Group"].ToString();
                            SPFieldUserValueCollection userValueColl = new SPFieldUserValueCollection(oSPWeb, usersString);

                            foreach (SPFieldUserValue userValue in userValueColl)
                            {
                                SPUser siteUser = userValue.User;
                                Console.WriteLine("User found: {0}", siteUser.Name);
                            }
                            break;
                        }
                    }
                }

Posted in Sharepoint | Tagged: , | 5 Comments »

Using SPWeb.EnsureUser(loginName) to add a new SPUser to a web

Posted by ken zheng on October 21, 2009

To add new user to the web is very common process but fails most time. See http://sharepointsearch.com/cs/blogs/sharepointblogs/archive/2007/12/20/using-spweb-ensureuser-loginname-to-add-a-new-spuser-to-a-web.aspx for more details.
The way to solve this problem is to user SPWeb.EnsureUser(loginName).The description in the SDK for EnsureUser is:
“Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.” Which happens to be exactly what we want!
Now we can finish our code:

   SPUser newUser = newWeb.EnsureUser(@"domain\username");
   newWeb.AllowUnsafeUpdates = true;

   // Create the new roleassignment that we want to add to the collection of roleassignments of the new web
   SPRoleAssignment roleAssignment = new SPRoleAssignment(newUser);
   SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
   // Add the binding to the correct roledefinition to the roleassignment
   // This can also be Contribute for contributor rights.
   // Keep in mind that in sites in other languages this needs to be translated
   roleDefBindings.Add(roleDefinitions["Read"]);
   roleAssignments.Add(roleAssignment);
   
   newWeb.AllowUnsafeUpdates = false;

   newWeb.Dispose();
   portalSite.Dispose();

Posted in Sharepoint | Tagged: , | 3 Comments »

Get Login Name by Full Name

Posted by ken zheng on July 15, 2009

Very often you need SPUser but only have Full Name of the user. By using AD Search you can get user login name.

using System.DirectoryServices;



const string ADPATH = "LDAP://myLDAPserver,validUserforAD";
const string USERNAME = "myDomain\\myUserName";
const string PASSWORD = "myPassword";
const string DOMAIN = "myDomain\\";

public static DirectoryEntry GetDirectoryObject()
{
        DirectoryEntry directoryObject = new DirectoryEntry(ADPATH, USERNAME, PASSWORD, AuthenticationTypes.Secure);
        return directoryObject;
}


public string GetUserNameByCompleteName(string completeName)
{
            DirectoryEntry adObject = GetDirectoryObject();

            //filter based on complete name
            DirectorySearcher searcher = new DirectorySearcher(adObject);
            searcher.Filter = "displayname=" + completeName;
            SearchResult result = searcher.FindOne();

            DirectoryEntry userInfo = result.GetDirectoryEntry();

            //getting user name
            string userName = (string)userInfo.Properties["samaccountname"].Value ?? string.Empty;
            userInfo.Close();
            adObject.Close();

            return DOMAIN + userName;
}

SPUser user = web.Site.RootWeb.SiteUsers[loginName]

Posted in Sharepoint | Tagged: | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers