July 28, 2016

Client Side People Picker with JSOM in Office 365/SharePoint 2013

Requirement:
In one of the project, I had a requirement to use people picker in Client side HTML which should have same features as SharePoint OOTB people picker provides.

PeoplePicker.png

Approach/Solution:
Follow below steps to achieve client side people picker functionality using JSOM.

1. Below mentioned JS files are required to load client side people picker using JSOM. All
JS
files are OOTB SharePoint JS files which are available under "_layouts/15" folder in
SharePoint.
- clienttemplates.js
- clientforms.js
- clientpeoplepicker.js
- autofill.js
- sp.js
- sp.runtime.js
- sp.core.js
2. Put reference of all JS files into HTML file for which you want to create Client Side People
Picker.
3. Add below Div tag in your HTML file.
<div id="peoplePickerDiv"></div>
4. Add below code in your custom JS file (custompeoplepicker.js) and also put reference of this file into HTML file.
        // Initialize People Picker.   
 $(document).ready(function() {  
 // Specify the unique ID of the DOM element where the  
 // picker will render.  
 initializePeoplePicker('peoplePickerDiv');  
  });  
 // Render and initialize the client-side People Picker.  
 function initializePeoplePicker(peoplePickerElementId) {  
  // Create a schema to store picker properties, and set the properties.  
  var schema = {};  
  schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';  
  //This value specifies where you would want to search for the valid values
  schema['SearchPrincipalSource'] = 15;             
  //This value specifies where you would want to resolve for the valid values
  schema['ResolvePrincipalSource'] = 15;  
schema['AllowMultipleValues'] = true;  
  schema['MaximumEntitySuggestions'] = 50;  
  schema['Width'] = '280px';  
  // Render and initialize the picker.  
// Pass the ID of the DOM element that contains the picker, an array of initial   
// PickerEntity objects to set the picker value, and a schema that defines  
// picker properties.    
this.SPClientPeoplePicker_InitStandaloneControlWrapper(
peoplePickerElementId, null, schema);  
                        }
                         // Get user information from client side people picker.
  // Query the picker for user information.

 function getUserInfo() {
  // Get the people picker object from the page.
  var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
var userInfo = '';
for (var i = 0; i < users.length; i++) {
var user = users[i];
for (var userProperty in user) {
userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
}
}
// Get user keys.
var keys = peoplePicker.GetAllUserKeys();
// Get the first user's ID by using the login name.
getUserId(users[0].Key);
}
// Get the user ID.
function getUserId(loginName) {
var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName);
context.load(this.user);
context.executeQueryAsync(
Function.createDelegate(null, ensureUserSuccess),
Function.createDelegate(null, onFail)
);
}
function ensureUserSuccess() {
console.log(this.user.get_id());
}
function onFail(sender, args) {
alert('Query failed. Error: ' + args.get_message());
}

// Below code sets values into client side people picker.
var divPeople = SPClientPeoplePicker.SPClientPeoplePickerDict.PeoplePickerDiv_TopSpan;
PeoplePickerDiv_TopSpan - Specify the unique ID of the DOM element where the picker will render.
var userObj = { 'Key': "sample@sample.com" };
divPeople.AddUnresolvedUser(userObj, true);

If you have any questions you can reach out our SharePoint Consulting team here.

Add Listview webpart from one site to any other site within Site collection in SharePoint.

Introduction: 
To display content from any SharePoint List/Library in Site Page or Publishing Page within same site, we can use List View web part but has one limitation: both Site Page where web part will be added and SharePoint List must be on same site. What if, there is requirement to have List view web part to display content of SharePoint List to another site, is it possible?

Here is the solution to display data from SharePoint list as List View web part from the site to its parent site within same site collection.

Approach/Solution:
  1. Open SharePoint site in Microsoft SharePoint Designer 2010 where your document library/list exists.
  2. Click on "Lists and Libraries" sections.
  3. Open your document library.
  4. Select "All Documents" view from "Views" section.
  5. Select your document library list view web part in design mode.

  6. Select "Web Part" tab in Ribbon.
  7. Click on "To Site Gallery" in "Save Web Part" group.
  8. Give appropriate name and description to web part and press "ok".
  9. Now your document library is added as web part in web part gallery. 
  10. You can add this document library web part to parent site or any other site within same site collection by adding it as list view web part on page.
  11. Web part will be available under "Miscellaneous" web parts category.
If you have any questions you can reach out our SharePoint Consulting team here.

July 8, 2016

Microsoft.Workflow.Client.ActivityValidationException: Workflow XAML failed validation when deploying solution with workflow

Problem:
I created the Workflow in SharePoint Designer 2013 and used "Save As Template" to export a workflow as WSP solution file into Site Assets library.  And then took the WSP file and deployed it to target environment.  However, when the workflow was updated, the "Feature" that enables/publishes the workflow was not activated in the target environment as previously. It was giving following exception: 

“Microsoft.Workflow.Client.ActivityValidationException: Workflow XAML failed validation due to the following errors:
Failed to create a 'ListId' from the text '$ListId:Lists/configuration;'. HTTP headers received from the server - ActivityId: 4b8fd97c-7c0f-4b12-8038-247a513a3f00. NodeId: . Scope: . Client ActivityId : 6768869d-f0de-3000-2122-c09145073f7d. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at Microsoft.Workflow.Common.AsyncResult.End[TAsyncRe……..”

Findings:
The "Elements.xml" file, which is packaged within the "WSP" solution file contains 2 different "<File>" elements. One <File> element represents the XAML file, and the other represents the Workflow association file. Comparing the Elements.xml file from a working solution file with the one which gives the specified error, I noticed that "WSPublishState" property value was different.  In the working solution file, WSPublishState value for each "<File>" element was "3" while in the failing version it was "1".

<PropertyName="WSPublishState" Value="3"/>

<PropertyName="WSPublishState" Value="1"/>
Note: WSPublishState property is also added within the "MetaInfo" tag in same file, so both occurrences should be updated.

Therefore, when the workflow files were published, due to different states it was corrupting the deployment process.

Resolution:
To resolve, please follow below steps:
1. Open WSP package using Project ->New Project -> ‘Import solution package’ from VS 2012/2013.
2. When solution is opened in VS 2012/2013, update the Elements.xml file.
3. Update the WSPublishState value for both "File" elements so they match. 

·       Note: Make sure you also update WSPublishState property within the ‘MetaInfo’ element if they differ. The actual value may matter (has not been tested); but if the values are both changed to "3" (to match the working version), then it works. Then again package the solution to create new WSP file, which will now deploy correctly.

The value of the WSPublishState corresponds to the 
Microsoft.SharePoint.WorkflowServices.StoragePublishState enumeration (viewed using Reflector):

internal enum StoragePublishState

{

    Unchanged,

    Unpublished,


    Published,


}

If you have any questions you can reach out our SharePoint Consulting team here.

July 6, 2016

Microsoft.Workflow.Client.ActivityValidationException: Workflow XAML failed validation due to the following errors: Failed to create a 'ListId' from the text

Problem:

I have created a Designer workflow for SharePoint 2013 List in development site and moved it to production environment. In Production environment, when I tried to activate the feature related to my custom workflow after uploading and activating the workflow package into the Solution Gallery, I encountered the error:

Microsoft.Workflow.Client.ActivityValidationException: Workflow XAML failed validation due to the following errors: Failed to create a 'ListId' from the text....

Cause:
I forgot to create one dependency List related to workflow solution in Production environment and tried to activate feature. Because of this, I encountered the above error.

To Solve this, I have created the dependency list and tried to activate the feature again, even this time I encountered the same error.

Resolution:
Because of the workflow instance that has already been created during the first erroneous deployment process, We need to delete that workflow instance through SharePoint designer and then re-activating the feature. This has solved my problem and workflow has been created successfully.


If you have any questions you can reach out our SharePoint Consulting team here.