Saturday 28 November 2015

"Views Likes Comments" in SharePoint 2010.


A month ago, I got a task to design functionality that provides Facebook features such as views, likes, and comments in SharePoint 2010 for our intranet portal.

In SharePoint 2010, there is no way to get such functionality out-of-the-box, but SharePoint 2013 provides it out-of-the-box.

I have created a simple SharePoint webpart to implement the functionality.

Steps to implement are:
1. Create a farm solution and add a web part.



2. Add the feature event receiver, constants, and CustomList file, and add the required images under the mapped image folder. The structure should be similar to that shown below (use standard naming conventions).


3. On the FeatureActivated method, call CustomList methods to create a list and add site columns to it.

4. Add a constants entry in the constants.cs.

5. In the webpart, add child controls for views, comments, and likes. And on page load event query the view count, comment count, comments, and like count from the lists..

6. Deploy the solution and activate the feature.


7. Edit the page and add a webpart to it as given below.


8. The page will look like this with a view count.

9. Click on "Like" to like the page. Here I have used the REST API of SharePoint 2010 using a list.svc you can find it in "like-newsArticle.js". There is a feature of like and unlike, and accordingly, the icon will change.


10. Then an entry will be added to the list - "Record Data List".


11. Click on the comments icon, then a div will slide down to enter, and press the comment button again. A REST query is used to save the data.


12. The comments entry will be saved into the list - "RecordDataCommentsList".


13. The final output is like this:


14. You can also edit the comments.


The views count is done on a per-user, per-page, per-day basis. If you refresh the page, the count will not increase.
The "Approve", "Edit" and "Reply" methods are now not implemented, but you can extend the functionality according to your use and needs.

I have uploaded the latest Visual Studio 2010 solution Likes.Comments.Views.REC.7z


🚀 "Happy Coding" 🚀

Saturday 31 October 2015

Set the Master Page of the Host Web in SharePoint Hosted App 2013 using JavaScript.

SharePoint CSOM (Client Side Object Model) or apps are capable of performing almost all functions that server-side code provides. There are some limitations, but the CSOM is maturing with time.

In this article, I will tell you how to set the master page of the host web from the app web (app) using JavaScript. Host web and App web reside in different contexts at different levels of scope, so SharePoint doesn't allow app web contexts to set host web master page directly.

Steps to set the master page of the host web from App Web:
1. Create a SharePoint hosted app and add a module named "CustomMasterPage".



2. Download a copy of seattle.master from the Master Page gallery.
3. Copy and paste this file content into our modules .txt file and make custom changes to it as required (I have just changed the background colour and image for the demo).

Tip: Don't change the extension file (.txt) in the module; otherwise, it is not easy to read the file content.

4. Now create a namespace to avoid method and property conflicts.
5. Get appweb and hostweb URLs.
6. Set clientContext and hostWebContext.
7. Read the text of the above created .txt file using a jQuery ajax call.
8. Create a file (by code) in host web and set that as the master page.
9. It will be at the site and system master page level.





SharePoint Hosted App JavaScript Code

"use strict";
 
// create namespace.
window.VB = window.VB || {};
 
window.VB.HostWebApp = function () {
    var hostWebUrl,
        appWebUrl,
        context,
        hostWebContext,
        destinationServerRelativeUrl,
        masterPageFileName,
        // read the master page contents from text file.
        readFileFromAppWeb = function (appPageUrl, hostWebServerRelativeUrl, fileName) {
            destinationServerRelativeUrl = hostWebServerRelativeUrl;
            masterPageFileName = fileName;
 
            $.ajax({
                url: appPageUrl,
                type: "GET",
                async: false,
                cache: false,
                success: function (data) {
                    if (data !== null && data !== undefined && data.length > 0) {
                        uploadFileToHostWeb(destinationServerRelativeUrl, masterPageFileName, data);
                    } else {
                        alert("Failed to read file from app web.");
                    }
                },
                error: function (jqXhr, textStatus) {
                    alert("Request for page in app web failed: " + textStatus);
                }
            });
        },
        // upload file to host web.
        uploadFileToHostWeb = function (serverRelativeUrl, filename, contents) {
            var createInfo = new SP.FileCreationInformation();
            createInfo.set_content(new SP.Base64EncodedByteArray());
            var index;
            for (index = 0; i < contents.length; index = index + 1) {
                createInfo.get_content().append(contents.charCodeAt(index));
            }
            createInfo.set_overwrite(true);
            createInfo.set_url(filename);
            var files = hostWebContext.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
 
            context.load(files);
            files.add(createInfo);
            context.executeQueryAsync(setMasterPageSuccess, setMasterPageError);
        },
        setMasterPageSuccess = function () {
            $("#message").append(["<br /><div>Master Page in host web successfully: ", destinationServerRelativeUrl, "/", masterPageFileName, "</div>"].join(""));
            var masterPageUrl = ["/", hostWebUrl.split("/")[3], "/", hostWebUrl.split("/")[4], "/", destinationServerRelativeUrl, "/", masterPageFileName].join("");
            setMaster(masterPageUrl);
        },
        setMasterPageError = function (sender, args) {
            alert('Request failed,Error: ' + args.get_message() + '\n' + args.get_stackTrace());
        },
        // set master page on host web.
        setMaster = function (masterUrl) {
            var hostWeb = hostWebContext.get_web();
            hostWeb.set_masterUrl(masterUrl);
            hostWeb.update();
 
            context.load(hostWeb);
            context.executeQueryAsync(onSetMasterPageSuccess, onSetMasterPageFail);
        },
        onSetMasterPageSuccess = function () {
            $("#message").append("<br /><div>Master page updated successfully..</div>");
        },
        onSetMasterPageFail = function (sender, args) {
            alert("Failed to update master page on host web. Error:" + args.get_message());
        },
        getQueryStringParameterValue = function (paramToRetrieve) {
            var params = document.URL.split("?")[1].split("&"),
                paramsLength = params.length,
                index;
            for (index = 0; index < paramsLength; index = index + 1) {
                var singleParam = params[index].split("=");
                if (singleParam[0] == paramToRetrieve) {
                    return singleParam[1];
                }
            }
        },
        init = function () {
            appWebUrl = decodeURIComponent(getQueryStringParameterValue("SPAppWebUrl"));
            hostWebUrl = decodeURIComponent(getQueryStringParameterValue("SPHostUrl"));
 
            //load the required scripts.
            var scriptbase = hostWebUrl + "/_layouts/15/";
            $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js",
                    function () {
                        //to execute cross domain request.
                        $.getScript(scriptbase + "SP.RequestExecutor.js");
                    }
                );
            });
        };
 
    return {
        execute: function () {
            init();
 
            context = new SP.ClientContext.get_current();
            hostWebContext = new SP.AppContextSite(context, hostWebUrl);
 
            readFileFromAppWeb(appWebUrl + "/_catalogs/masterpage/CustomAppMaster.txt""_catalogs/masterpage""CustomAppMasterPage.master");
        }
    };
}();
 
(function () {
    window.VB.HostWebApp.execute();
}());

This code will work both for setting the Master Page of HostWeb and AppWeb by using appropriate context.

🚀 "Happy Coding" 🚀

Saturday 10 October 2015

SharePoint activates the feature using the REST API.

The REST API provides a simple and easy way to activate features at the site and web level..

I have created simple JavaScript code to activate the feature.

function activateFeature() {
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/features/add('7e2e2482-9007-4db3-8a7f-9bccd986ec4e')",
        type: "POST",
        headers: {
            "Accept": "application/json;odata=verbose", //return data format.
           "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
// perform operation on the response object.
        },
        error: function (err) {
            alert("Error: " + JSON.stringify(err));
        }
    });
}


Change the URI, and the feature will be activated based on the endpoint.

  1. /web - then feature will be activated at the web level.
  2. /site - then feature will be activated at the site collection level.

Now pass the feature GUID into the add method. You can get the GUID value by using the PowerShell command or by "SharePoint Manager 2013".


🚀 "Happy Coding" 🚀

Saturday 8 August 2015

SharePoint 2013 REST API Get requests from the designer workflow.

SharePoint 2013 provides a completely new workflow engine, which has a new "Call HTTP web service" action that enables us to communicate with the web service from our workflow.

In this blog post, I will show you how you can use this new action method for a REST API GET request to a SharePoint list.

I have created a list "WebServiceList" and inserted dummy data into it as shown below.


Open SharePoint Designer 2013 and create a workflow attached to the list created above. Use the platform type as SharePoint 2013 workflow; otherwise Call HTTP web service method will not be available.
In our normal CSOM(Client Side Object Model), we call the REST API to get data back in JSON format. You need these headers as:

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose

In the workflow add the "Build the Dictionary" action of above headers as:

Click on the three dots to add items to the dictionary.






Now, add the HTTP call web service method with type GET as given below:


Test your REST call in the browser as given below:

Add the header dictionary variable created just above to the web service and set the request type to "GET" as:


Then click OK.
Add the GET action, and now we will get the result from the response content variable as

Now get the CountItems of the Output variable (allTitles) from the get action.
Then take one variable and set it to "0" and insert Loop (Loop n times). Inside the loop again, add the GET action to get all titles from the variable JsonResult and log it. Add the Calculate action to add "1" to the loop index variable and then again set its value. Now end the workflow.
Complete workflow is given below


See the Tasks and Workflow History list details below; you will get the data fetched from the source list is logged in the history list.

Hope this will help you!


🚀"Happy Coding"🚀