SharePoint Tips

Saturday, June 23, 2018

SharePoint hosted Add-In : Read list data : JSOM


In last post (Step by step guide to create “SharePointHosted Add-In” in SharePoint Online (Office 365)), I created a basic SharePoint hosted Add-In as shown below.


Now, I’ll add logic to this Add-In to read data from a SharePoint list (VendorList) as shown below.


Step 1- Open Default.aspx file and add new div as shown below.


Step 2- Open App.js file. Replace code in js file with code given below.


Changes in App.js file
var Items = null;
'use strict';
var listItems;
var hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ListItems)
});

function ListItems() {
    var context = new SP.ClientContext(appweburl);
    var ContextSite = new SP.AppContextSite(context, hostweburl);
    var web = ContextSite.get_web();
    context.load(web);
    var list = web.get_lists().getByTitle('VendorList');
    var caml = new SP.CamlQuery();
    caml.set_viewXml("<View><Query></Query></View>"); // U can change the query for getting desired result  
    Items = list.getItems(caml);
    context.load(Items);
    context.executeQueryAsync(onSucceededCallback, onFailedCallback);
}

function onSucceededCallback(sender, args) {
    var enumerator = Items.getEnumerator();
    var myText = 'Items in VendorList: <br><br>';
    while (enumerator.moveNext()) {
        var listItem = enumerator.get_current();
        myText += 'Title: ' + listItem.get_item('Title') + '<br>';
        myText += 'Address: ' + listItem.get_item('Address') + '<br><br>';
    }
    myDiv.innerHTML = myText;
}

function onFailedCallback(sender, args) {
    var myText = '<p>The request failed: <br>';
    myText += 'Message: ' + args.get_message() + '<br>';
    myDiv.innerHTML = myText;
}

function getQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) return singleParam[1];
    }
}

Step 3 – Deploy your Add-In after all changes.


Step 4- You may get below error as your Add-In don’t have enough permissions.


Step 5- For simplicity we are providing Full Control at Site Collection level. You can also provide only Read access.


Step 6- Now deploy Add-In again and you’ll get below screen.


Step 7- Trust Add-In by clicking ‘Trust It’ button.

Here is the Add-In with data from VendorList


Friday, June 22, 2018

Step by step guide to create “SharePoint Hosted Add-In” in SharePoint Online (Office 365)

In earlier post (Step by step guide to create “Developer Site” for deploying SharePoint Add-Ins in SharePoint Online (Office 365)), I showed you how to create SharePoint online 'Developer Site'. In this post I'll show you how create a basic SharePoint hosted Add-In / App.

Step 1- Open Visual Studio and create new project of type ‘SharePoint Add-In’ as shown below.


Step 2- Provide url of your SharePoint online ‘Developer Site’ and select ‘SharePoint-hosted’ as shown below.



Step 3- Sign in to your Office 365 account with your credentials.




Step 4- Select version as ‘SharePoint Online’


Step 5- Wait for project to be created.



Step 6 – Following files are generated with new project.


Step 7- Deploy the Add-In to SharePoint online site by clicking on Start button



Step 8- Enter your Office 365 credentials.



Step 9 – Your SharePoint hosted Add-In is created.










Thursday, June 21, 2018

Step by step guide to create “Developer Site” for deploying SharePoint Add-Ins in SharePoint Online (Office 365)

Below are the steps to create SharePoint Online Developer Site for creating, deploying and testing SharePoint Apps/ Add-Ins.

Step 1- Login to Office 365 and go to sites list as shown below. Click on button ‘Add a site’.


Step 2- Create Site Collection page will open. Add Title of your Developer Site and give name. Select site template ‘Developer Site’ as shown below and click OK button.




Step 3- It will take few minutes to create new site collections. Once created, link in below image will be active.


Step 4- This is our new Developer Site for creating and deploying add-ins.



Tuesday, January 16, 2018

SharePoint Search Query CollapseSpecification

Collapse similar search results using the CollapseSpecification property


The CollapseSpecification property takes a Spec parameter that can contain multiple fields separated either by a comma or a space, which evaluated together specify a set of criteria used for collapsing.

Syntax: CollapseSpecification = Spec

Table 1. Spec parameter fields
Element in parameterDescription
Spec Subspec(<space>Subspec)* 
Subspec Prop(','Prop)*[':'Dups] 
Prop A valid managed property or an alias of a managed property. Prop is case-insensitive. The managed property must be queryable and either sortable or refineable. 
Dups An integer specifying the number of items to retain. The default value is 1. 

Properties are combined by using the OR operator. 
, Properties are combined by using the AND operator. 
* Indicates more items. 
() or [] Indicates optional items. 
If the fields in Spec are separated by commas, the fields are combined by using the AND operator. If all of the specified fields are matched, the items are collapsed.
In contrast, if the fields in Spec are separated by spaces, the fields (or Subspecs) are combined by using an expansion that includes both the AND operator and ORoperator. For example, an expression such as Category:3 Product:2 is internally transformed to the following expression (Category AND Product) OR (Category) with a counter for each; hence a maximum of two of the former and three of the latter. Items are collapsed if some of the specified fields are matched.
Ref: https://docs.microsoft.com/en-us/sharepoint/dev/general-development/customizing-search-results-in-sharepoint

Tuesday, January 9, 2018

Tools for writing and debugging RegEx



·         RegExr (for JavaScript)
·         Perl: YAPE: Regex Explain
·         Regex Coach (engine backed by CL-PPCRE)
·         RegexPal (for JavaScript)
·         Regex Buddy
·         Regex 101 (for PCRE, JavaScript, Python, Golang)
·         Visual RegExp
·         Expresso (for .NET)
·         Rubular (for Ruby)
·         Regular Expression Library (Predefined Regexes for common scenarios)
·         Txt2RE
·         Regex Tester (for JavaScript)
·         Regex Storm (for .NET)


Books


·         Mastering Regular Expressions, the 2nd Edition, and the 3rd edition.
·         Regex Cookbook

Free resources

·         Regular Expressions - Everything you should know (PDF Series)
·         Regex Syntax Summary
·         How Regexes Work