Wednesday, September 17, 2014

Updating UserMulti field using Sharepoint 2013 REST API

To update User field via Sharepoint 2013 REST API one just needs to provide ID of the user in the JSON of the element being updated.

For example, say we have list Email and it contains field Recipient which is a user field. When you query this list using Sharepoint 2013 REST API you will get RecipientId field instead. This is the clue to updating the field - you just need to provide ID of the user you want to be a recipient.



var el = { 
 "__metadata": { "type": "SP.Data.EmailListItem" },
 Title: "Title of the message", 
 Body" "Body of the message", 
 RecipientId: 5 
 };

The ID of the user cam be obtained via EnsureUser function.

When your Recipients field has type of UserMulti you need to deal with the array of user IDs. You will need to specify also metadata for the array type: Collection(Edm.Int32).

var el = { 
 "__metadata": { "type": "SP.Data.EmailListItem" },
 Title: "Title of the message", 
 Body" "Body of the message", 
 RecipientsId: { "__metadata": { "type": "Collection(Edm.Int32)" }, "results": [5,4,6] },
 };

Friday, December 6, 2013

Custom People Picker in Sharepoint 2013 Online

Sharepoint 2013 introduced new elegant people picker out of the box. You can take a look at it when you click "Share" link in the header of your site. Once you start typing user name in the textbox, the search results show up immediately in the list below.

This picker consolidates user and group entries from AD as well as Sharepoint site. Very convenient!

Ever wondered if you can build your own custom people picker using the same functionality. Yes, you can!

All you need is a textbox and a few lines of JavaScript code.

You also need to include JQuery and JQuery UI libraries somewhere on your page.


Let's start with the text box:
 

<input ID="txtPeoplePicker" class="people" type="text"/>

 
Next, this textbox needs to be converted to autocomplete box using JQuery UI:

 
<script type="text/javascript" language="javascript">
 
    $(document).ready(function () {
       
        $(".people").autocomplete({
 
            source: PeoplePickerSource,
            minLength: 2
 
        });
    });
  
</script>

 

As you can see, parameter "source" is a function PeoplePickerSource that is called by JQuery autocomplete  plug-in to retrieve data from the Sharepoint people picker web service. This function is defined in the following JS file:
 

 
function PeoplePickerSource ( request, response ) {
    $.ajax({
        url: "/_vti_bin/client.svc/ProcessQuery",
        dataType: "json",
        type: "POST",
        data: peoplePickerXML(request.term),
        headers: {
            "Accept": "*/*",
            "Content-Type": "text/xml",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
 
            response($.map(JSON.parse(data[2]), function (item) {
                return {
                    label: item.DisplayText,
                    value: item.DisplayText
                }
            }));
 
        },
        error: function (data) {
            
            console.log(data.responseText);
        }
    });
}
 
 
 
 
function peoplePickerXML(search) {
 
    return '<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName="Javascript Library">' +
    '<Actions><StaticMethod TypeId="{de2db963-8bab-4fb4-8a58-611aebc5254b}" Name="ClientPeoplePickerSearchUser" Id="0">' +
    '<Parameters><Parameter TypeId="{ac9358c6-e9b1-4514-bf6e-106acbfb19ce}">' +
    '<Property Name="AllowEmailAddresses" Type="Boolean">false</Property>' +
    '<Property Name="AllowMultipleEntities" Type="Boolean">true</Property>' +
    '<Property Name="AllUrlZones" Type="Boolean">false</Property>' +
    '<Property Name="EnabledClaimProviders" Type="Null" />' +
    '<Property Name="ForceClaims" Type="Boolean">false</Property>' +
    '<Property Name="MaximumEntitySuggestions" Type="Number">30</Property>' +
    '<Property Name="PrincipalSource" Type="Number">15</Property>' +
    '<Property Name="PrincipalType" Type="Number">13</Property>' +
    '<Property Name="QueryString" Type="String">' + search + '</Property>' +
    '<Property Name="Required" Type="Boolean">true</Property>' +
    '<Property Name="SharePointGroupID" Type="Number">0</Property>' +
    '<Property Name="UrlZone" Type="Number">0</Property>' +
    '<Property Name="UrlZoneSpecified" Type="Boolean">false</Property>' +
    '<Property Name="Web" Type="Null" />' +
    '<Property Name="WebApplicationID" Type="String">{00000000-0000-0000-0000-000000000000}</Property>' +
    '</Parameter></Parameters></StaticMethod></Actions><ObjectPaths /></Request>';
 
}

 Funstion peoplePickerXML provides XML payload to the web service and among other parameters, specifies search string.

Once everything is linked together on the page, you will get your custom autocomplete people picker.

That is all folks! Enjoy!

 

Friday, December 16, 2011

Add PeopleEditor Control to your Application Page

You can use this PeopleEditor control when developing application pages or control templates using the following syntax:


But only after you have added a directive at the top of the application page or control template:

Reposted from http://karinebosch.wordpress.com/sharepoint-controls/peopleeditor-control/

Tuesday, December 13, 2011

Attach Debug - View IIS worker process list

In many cases you may have multiple web applications running on the same machine simultaneously. When using attach to process debugging approach, you need to know the exact process id of the application you are debugging. This command will do this work for you:
c:\windows\system32\inetsrv\appcmd.exe list wp

Recycle IIS Application Pool:Error occurred in deployment step 'Recycle IIS Application Pool': List does not exist.

This error happens often on the development Sharepoint 2010 machine after upgrade from sharepoint 2007 when trying to deploy solution from Visual Studio. In my case, the problem was related to incorrect upgrade of sharepoint publishing feature. It was upgraded with the feature activated. To correct the issue, it was sufficient to deactivate the publishing feature and reactivate it.

Sunday, August 21, 2011

Sharepoint Workflow Associations and Status Column

While migrating from Sharepoint 2007 to Sharepoint 2010 there was a custom workflow which used workflow status column in its logic. It was critical to have this column created when associating workflow with the list. Initially using standard technique of adding workflow association I could not get this column created automatically. The trick was to have StartCreate property set to true when creating workflow association.

Tuesday, July 26, 2011

Real World Branding with SharePoint 2010 Publishing Sites

http://msdn.microsoft.com/en-us/library/gg430141.aspx

Real World Branding with Sharepoint 2010 Publishing

Publishing features in Sharepoint 2010 or Sharepoint foundation. Walks through sample Adventure works site. How to create master page.