Friday, May 17, 2013

SharePoint 2013 - Getting UserName from Person Column in List using JSOM

Here is the code to retrieve person data and convert to string. I have created OrgChart List with Name Column and it's type is Person or Group.

function retrieveListItems()
        {
            clientContext = new SP.ClientContext.get_current();
            //oWeb = clientContext.get_web();
            //oLists = oWeb.get_lists();
            //clientContext.load(oLists, 'Include(Title, Id)');
            //oList = oLists.getByTitle("OrgChart");
            oList = clientContext.get_web().get_lists().getByTitle('OrgChart');
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml(
                '' +
                '1
' +                '25
'            );
            oListItems = oList.getItems(camlQuery);
            clientContext.load(oListItems);
            clientContext.executeQueryAsync(
                Function.createDelegate(this,this.onListSuceeded),
                Function.createDelegate(this, this.onListFailed)
                );
        }


function onListSuceeded(sender, args)
        {
            alert("onListSuceeded");
            var EnumerateListItems = oListItems.getEnumerator();
            while (EnumerateListItems.moveNext())
            {
                var oListItem = EnumerateListItems.get_current();
                var assignedToVal = oListItem.get_item('Name');
                var userName = "";
                if (assignedToVal != null)
                    userName = assignedToVal.get_lookupValue();
                alert(userName);
            }
        }


No comments:

Post a Comment