Ads

Showing posts with label People Picker. Show all posts
Showing posts with label People Picker. Show all posts

Saturday, 31 May 2014

PeopleEditor control to trigger CheckNames click on focusout event in clientside

For example in your aspx page having one PeopleEditor control .But you need to resolve the names in client side.
First you have to check whether the PeopleEditor control having any value or not.
This scenario we can use the _hiddenSpanData which is one of  the hidden input control for the PeopleEditor control .This control id will be like “PeopleEditor control id__hiddenSpanData”.It will be having the value which we entered in the PeopleEditor control.
If Editor control having value then trigger the CheckName anchor button. For this we can use _checkNames. This control id will be like “PeopleEditor control id_checkNames”.
Here is the sample code:
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
<title>PeopleEditor Page</title>
<script type="text/javascript" language="javascript">
function TriggerCheckNameClick()
{
var pickerval = document.getElementById('<%=PplEditorCtrl.ClientID%>' + "_hiddenSpanData").value;
pickerval = pickerval.replace(/&nbsp;/gi, '');
if ((pickerval != null) && (pickerval != '') && (pickerval != ' ')) {
document.getElementById('<%=PplEditorCtrl.ClientID%>' + "_checkNames").click();
}
}
</script>
</head>
<body style="margin:0px; padding:0px">
<form id="form2" runat="server">
<div>
      <table width="100%">
         <tr>
              <td>
<cc1:PeopleEditor ID="PplEditorCtrl" MultiSelect="true" runat="server" SelectionSet="User" AllowEmpty="true" Width="500" onfocusout="TriggerCheckNameClick()"/>
</td>
       </tr>
   </table>
</div>
</form>
</body>
</html>

Monday, 8 July 2013

Using People Picker control in edit web part Properties

How to use People Picker control in edit web part Properties?

First Create a "Empty SharePoint Project". Add New "Web Part" named "TestWebpart". Create a new webpart property in this, as shown below.

private string _ImpersonateUser = null;

[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(false)]
[System.ComponentModel.Category("Custom Properties")]
[WebDisplayName("Impersonate User")]
[WebDescription("User for impersonation")]
        public string ImpersonateUser
        {
            get
            {
                return _ImpersonateUser;
            }
            set
            {
                _ImpersonateUser = value;
            }
        }


Leave this as it is, we will come back to this again.


Add new class named "PeoplePickerEditor.cs" to this project. Your class should look like:

namespace SP.Anmol.Customization
{
    public class PeoplePickerEditor: EditorPart
    {
        private PeopleEditor _peoplePicker;

        public PeoplePickerEditor(string webPartID)
        {
            this.ID = "PeoplePickerEditor" + webPartID;
            this.Title = "Impersonate User";
        }


        protected override void CreateChildControls()
        {
            _peoplePicker = new PeopleEditor();
            _peoplePicker.ID = "pe1";
            _peoplePicker.AllowTypeIn = true;
            _peoplePicker.AllowEmpty = false;
            _peoplePicker.MultiSelect = false;
            _peoplePicker.Width = Unit.Pixel(250);
            _peoplePicker.SelectionSet = PeopleEditor.AccountType.User.ToString();

            Controls.Add(_peoplePicker);
        }


        public override bool ApplyChanges()
        {
            EnsureChildControls();
            TestWebpart.TestWebpart webPart = WebPartToEdit as TestWebpart.TestWebpart;
            if (webPart != null)
            {

                //set value of web part property
                webPart.ImpersonateUser = _peoplePicker.CommaSeparatedAccounts;
            }
            return true;
        }


        public override void SyncChanges()
        {
            EnsureChildControls();
            TestWebpart.TestWebpart webPart = WebPartToEdit as TestWebpart.TestWebpart;
            if (webPart != null)
            {

                //set value back to people picker control
                _peoplePicker.CommaSeparatedAccounts = webPart.ImpersonateUser;
            }
        }


    }
}

Now get back to web part code file. Implement the interface IWebEditable. Your web part code looks like:

namespace SP.Anmol.TestWebpart
{


    [Guid("84B6AF3B-9BA4-440A-AA4A-9657A5D67798")]
    public class TestWebpart : Microsoft.SharePoint.WebPartPages.WebPart, IWebEditable
    {
        public AnonymousUpload()
        {
            this.ExportMode = WebPartExportMode.All;
        }

      
        private string _ImpersonateUser = null;

      
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        [System.ComponentModel.Category("Custom Properties")]
        [WebDisplayName("Impersonate User")]
        [WebDescription("User for impersonation")]
        public string ImpersonateUser
        {
            get
            {
                return _ImpersonateUser;
            }
            set
            {
                _ImpersonateUser = value;
            }
        }

      
        protected override void CreateChildControls()
        {
                 //Place your logic here
        }


 

        //Methods to be implement for interface IWebEditable        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();
            editors.Add(new PeoplePickerEditor(this.ID));

            return new EditorPartCollection(editors); 
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }


    }
}

Friday, 10 May 2013

SharePoint PeopleEditor Control, people picker

This tip demonstrates-
      How to Populate SharePoint PeopleEditor Control .
      How to get selected user from SharePoint PeopleEditor Control .

In .aspx file
<SharePoint:PeopleEditor ID="user" runat="server" SelectionSet="User" AllowEmpty="false" Enabled="false" MultiSelect="false" ValidatorEnabled="false" />
*Here MultiSelect property is set to false which allow only single user selection.

In .cs file
Method for populating SharePoint:PeopleEditor control with logged in User Name.
private void PopulatePeopleEditor()
{
PickerEntity entity;
SPSite site;
SPWeb web;

try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
entity = new PickerEntity();
entity.Key = web.CurrentUser.ID;
//Setting users display name as display text.
entity.DisplayText = web.CurrentUser.Name;
entity.IsResolved = true;
user.Entities.Add(entity);
}
}

}
catch { }
}

Method for getting the selected user from the SharePoint:PeopleEditor control .

private void GetSelectedUser()
{
string userId = string.Empty;

PickerEntity selectedEntity;
SPSitesite;
SPWeb web;
SPUser selectedUser;

try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
if (user.ResolvedEntities.Count > 0)
{
selectedEntity = (PickerEntity)user.ResolvedEntities[0];
userId = selectedEntity.Key;
}
if(!string.IsNullOrEmpty(userId))
selectedUser = web.SiteUsers.GetByID(Convert.ToInt32(userId ));
}
}

}
catch { }
}

Getting AccountName from a "Person or Group" field


This is one I ran into a while back and blogged for reference:

Let's say you have a "Person or Group" field on a SharePoint list... How do you get the username of that user in code? Finally found the answer to this buried in MSDN.
Here is the code snippet to convert "Person or Group" type field in a SharePoint list(which is stored like: "12345#;Some Name") into the correct account name in the form of “<Domain>\<Username>”.

SPFieldUserValue userValue = new SPFieldUserValue(workflowProperties.Web,retrievedFieldValue);

string userName = userValue.User.LoginName;

In this case the SPWeb is coming from the workflowProperties, but it just needs the one that the SPList lives in.

Ads