August 29, 2013

SharePoint Add People Picker using C or aspx, get SPUser from PeoplePicker, Save User and Read pragramatically from/to SPList / Application Page using C#

Create People Picker at run-time using SharePoint C# code, Programmatically.

protected void CreatePeoplePicker()  
        permissionPplPicker = new PeopleEditor();  
        permissionPplPicker.MultiSelect = true;  
        permissionPplPicker.AllowEmpty = false;  
        permissionPplPicker.AutoPostBack = true;  
        permissionPplPicker.PlaceButtonsUnderEntityEditor = true;  
        permissionPplPicker.ID = "PeopleEditor";  
        permissionPplPicker.SelectionSet = "User,SPGroup";  
        permissionPplPicker.Rows = 1;  
        permissionPplPicker.BorderStyle = BorderStyle.Outset;  
         //To apply style to people picker or Add SP default css file  
        LiteralControl styleControl = new LiteralControl();  
        styleControl.Text = "<style type='text/css'>.ms-inputuserfield{ font-size:8pt; font-family:Verdana,sans-serif; border:1px solid #a5a5a5;} a{color:#000000;text-decoration: none;font-weight:normal;font-style:normal;}{padding-left:1px;padding-top:2px;}</style> ";  

Create People picker SharePoint Application page .aspx Page ,

<table style="MARGIN-TOP: 8px" class='ms-formtable' border='0' cellspacing='0' cellpadding='0' width="100%">  
         <td class='ms-formlabel' valign='top' width='190' nowrap>  
           <h3 class='ms-standardheader'>  
             <nobr>People picker control<span class='ms-formvalidation' title="This is Required filed."> *</span></nobr>  
         <td class='ms-formbody' valign='top'><span dir='none'>  
           <SharePoint:PeopleEditor ID=" pplPicker " runat="server" AutoPostBack="false" MultiSelect="false" MaximumEntities="1" ValidatorEnabled="true" SelectionSet="User" CssClass="ms-long" Width="99%" EnableViewState="true" />  
           <SharePoint:InputFormRequiredFieldValidator ID="ADValidator" runat="server" Display="Dynamic" SetFocusOnError="true"  
             ControlToValidate="pplPicker" BreakBefore="true" ErrorMessage="You must specify a value for this required field." />  

Get SPUser or SharePoint web user from People picker Programmatically C#

SPUser oUser = null;

if (pplPicker.Entities.Count > 0)
        PickerEntity ppl = (PickerEntity) pplPicker.Entities[0];
        oUser = SPContext.Current.Web.EnsureUser(ppl.DisplayText);

For more than one User we can iterate pplPicker.Entities collection in foreach loop.

Or you can use ResolvedEntities in place of .Entities

Save or update people picker User / SPUser programmatically in/to SharePoint List Item

using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
      using (SPWeb oWeb = oSite.OpenWeb("//Site Relative Url//", false))
    /*Getting Item by List Item ID and saving User to the list */
    SPListItem user = oWeb.Lists["User List"].GetItemById(Convert.ToInt32(ItemID))

    /* oUsercan be get from people picker by referring above code*/
    user["Team Lead"] = oUser;
    adminWeb.AllowUnsafeUpdates = true;
  catch(SPException ex){ }
   adminWeb.AllowUnsafeUpdates = false;

Get SharePoint user / SPUser from SharePoint List using C# / Programmatically

/* Calling GetSPUserMethod to get User from List*/

SPListItem oItem = oWeb.Lists["User List"].GetItemById(Convert.ToInt32(ItemID));
SPUser oUser = GetSPUser(oItem, "User");
protected SPUser GetSPUser(SPListItem item, string user)
            SPUser oUser = null;
            SPFieldUserValue userValue = new SPFieldUserValue(item.Web, Convert.ToString(item[user]));

            if (userValue != null)
                oUser = userValue.User;
            return oUser;

Praveen Pandit
Keep Sharing Knowledge!!

No comments:

Post a Comment