Tuesday, February 2, 2016

Develop a SharePoint-hosted SharePoint Add-in to get user's permission at the site level using JavaScript object model (JSOM).

This article is a basic example of developing a SharePoint-hosted SharePoint Add-in and deploying to SharePoint online site

Source Code

Environment: Office 365, SharePoint Online, Visual Studio 2015

Prerequisite: Active users are added to the Office 365 site and granted permission to access the SharePoint online.


Requirement: Develop a SharePoint-hosted SharePoint Add-in to get user's permission at the site level using JavaScript object model (JSOM).

Solution:
Create the add-in project


  1. Start Visual Studio
  2. Choose File > New > New Project.
  3. In the New Project dialog box, expand the Visual C# node, expand the Office/SharePoint node, and then choose Add-ins > Add-in for SharePoint.
  4. Name the project, and then choose OK.




  1. Specify the Add-in for SharePoint Settings dialog box, provide the full URL of the SharePoint site that you want to use to debug your add-in. This is the URL of the Developer Site. (Use HTTPS, not HTTP in the URL.) Under How do you want to host your SharePoint Add-in, choose SharePoint-hosted. Choose Finish.




  1. Provide your subscription administrator's credential if prompted to login to your Developer Site.




  1. In Specify the target SharePoint version page, select SharePoint Online, and then click Finish button




Code your add-in


Following is the project items created by default template. Refer to this MSDN article for more information about each project item


Publish the Add-in




Deploy the Add-in to the developer site.




Developer Site home page with the Add-ins in Testing list




Run the add-in

Note: User running this app should have appropriate permission to get the permission level for other users. A full control permission is recommended in this case.




Source Code is available at TechNet gallery.

Sunday, January 31, 2016

A different version of this App is already installed with the same version number. You need to delete the app from the site and the site recycle bin to install this version.

Environment: Office365, SharePoint Online, Visual Studio 2015

Requirement: Re-Deploy an SharePoint Add-in into SharePoint Online Developer site

Approach: The Add-in is build successfully, but was getting an error when it is deployed using Visual Studio or added to Apps list in Developer site.

Issue/Error: A different version of this App is already installed with the same version number.  You need to delete the app from the site and the site recycle bin to install this version.

Resolution: We could not delete the app as it was neither in site recycle bin nor second-stage recycle bin. Clean and Retract the app from Visual Studio. Redeploy it again after successful retraction.

Wednesday, January 6, 2016

Add text or image between suite bar in Ribbon

Environment: SharePoint 2013 Enterprise

Requirement: Add text or image between suite bars

Solution:

Edit the master page using SharePoint Designer and add <div> tags with banner text or images as required. The following code snippet is from the out of the box seattle.master page. Text that are highlighted are inserted between the suite bars.

<div id="suiteBar" class="ms-dialogHidden noindex" >
  <div id="suiteBarLeft">
  <div class="ms-table ms-fullWidth">
     <div class="ms-tableRow">
        <div class="ms-tableCell ms-verticalAlignMiddle">
                <SharePoint:DelegateControl id="ID_SuiteBarBrandingDelegate" ControlId="SuiteBarBrandingDelegate" runat="server"/>
        </div>
        <div class="ms-tableCell ms-verticalAlignMiddle" style="text-align:center;font-weight:bold;color:#FF3300;"> [[[ SOME BANNER IMAGES ]]] </div>
        <SharePoint:AjaxDelta runat="server" id="DeltaSuiteLinks" BlockElement="true" CssClass="ms-core-deltaSuiteLinks">
            <div id="suiteLinksBox">
                <SharePoint:DelegateControl id="ID_SuiteLinksDelegate" ControlId="SuiteLinksDelegate" runat="server" />
            </div>                                                                                                              
         </SharePoint:AjaxDelta>
         <div class="ms-tableCell ms-verticalAlignMiddle" style="text-align:center; font-weight:bold;color:#FF3300;"> [[[ SOME BANNER TEXT ]]] </div>
      </div>
  </div>
</div>
To insert a row below the Suite bar

<SharePoint:ThemedClusteredHoverImage
                                    runat="server"
                                    ……………………
</span>
</div>
</SharePoint:AjaxDelta>
</div>
</div>
<div id="s4-ribbonrow ms-verticalAlignMiddle" style="text-align:center;font-size:large;font-weight:bold; background-color:#6699FF;">[[ SOME SAMPLE TEXT/BANNER ]]</div>
<div id="ms-hcTest"></div>
<div id="s4-ribbonrow">
<div id="globalNavBox" class="noindex">
<div id="ribbonBox">
  <div id="s4-ribboncont">
  <SharePoint:AjaxDelta id="DeltaSPRibbon" BlockElement="true" runat="server">
                <SharePoint:DelegateControl runat="server" ID="GlobalDelegate0" ControlId="GlobalSiteLink0" />
                  <SharePoint:SPRibbon
                                    runat="server"
                                    PlaceholderElementId="RibbonContainer"
                                    CssFile="" >
                                    <SharePoint:SPRibbonPeripheralContent
                                      runat="server"
                                      CssClass="ms-core-defaultFont ms-dialogHidden"
                                      Location="TabRowLeft">
                                    </SharePoint:SPRibbonPeripheralContent>
P.S. Create appropriate CSS classes and add to the style sheet.

Tuesday, December 8, 2015

Get next item ID from a SharePoint Document Library using REST

Environment: Using jQuery, REST, SharePoint 2013 Enterprise, knockout JS

Requirement: To get the next item ID from a SharePoint Document Library using REST

Prerequisite: knockout and latest jQuery are defined in the code.

Code:

function getNextSequenceID() {
              
var documentLibraryName = 'DocumentLibraryName';     
               var serverUrl = window.top._spPageContextInfo.webServerRelativeUrl;
              
               //Query the top 1 item sorted in descending order.
               var fileListItemIDUri = serverUrl + "/_api/web/lists/getbytitle('" + documentLibraryName + "')/items?$select=ID&$top=1&$orderby=ID%20desc";
               var sequenceID = 1;
                                            
                              return $.ajax({
                                             url: fileListItemIDUri,
                                             type: "GET",
                                             headers: { "accept": "application/json;odata=verbose" },
                                             success: function (data) {
                                             if(data.d.results.length==1)
                                             {
                                                            var id = data.d.results[0].ID;
                                                            sequenceID = id+1;
                                             }   
alert(“The next item ID is :”+ sequenceID);

        },
        error: function (data) {
        },
        });

};

Create a folder in a document library using REST

Environment: Using jQuery, REST, SharePoint 2013 Enterprise, knockout JS

Requirement: Create a folder in a document library with current year as the folder name using REST. Check if folder exists already, else create a new folder.

Prerequisite: knockout and latest jQuery must be defined.

Code:
var currentYear = ko.observable();
var documentLibraryName = “DocumentLibraryName”;   
var serverUrl = “http://webServerURL”;

//Following code should be executed on page load
self.currentYear = ko.computed(function () {
               currentYear = (new Date).getFullYear();
               return (new Date).getFullYear();
});

//fileUpload method is used to upload a file to a folder in SharePoint document library
self.fileUpload = function () {
self.doesFolderExist();
….
….
};

//doesFolderExist method is used to check if the folder already exists in the document library and create a new folder if does not exist.
self.doesFolderExist = function (){
                                            
              
               var fileCollectionEndpoint = serverUrl + "/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName +"/"+currentYear+ "')";
               return $.ajax({
               url: fileCollectionEndpoint,
               type: "GET",                      
               headers: {
                              "accept": "application/json;odata=verbose",
                              "X-RequestDigest": window.top.$("#__REQUESTDIGEST").val()
                              },
                              success: function (data) {
                                             alert("Folder already exists");
                      },
               error: function (data) {
                              //Folder does not exist, so create a new folder
                              self.createFolder();
        }
});
};


//Create a folder in document library
self.createFolder = function ()
{
                             
               var fileCollectionEndpoint = serverUrl + "/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName + "')/folders/add(url=\'" + currentYear + "\')";
                                            
               return $.ajax({
                              url: fileCollectionEndpoint,
                              type: "POST",
                              headers: {
                                             "X-RequestDigest":  window.top.$("#__REQUESTDIGEST").val(),
                                             "accept": "application/json;odata=verbose",
                                             "content-type": "application/json;odata=verbose"
                              },
                              success: function (data) {
                                             //alert(‘Folder created successfully’);
                                             },
                                              error: function (jqXHR, textStatus, errorThrown) {
                                                            //handle error...
                                                            alert('Failed to create folder in library.');
                                             }
                              });
               };

References:

Tuesday, November 10, 2015

Install Windows Server 2012 and SharePoint 2016 IT Preview on Virtual Machine

Introduction
This article provides instructions to setup a Virtual machine to host SharePoint Server 2016 IT Preview as standalone environment. In this example we are using VM Player 7 to host SP2016.

Steps:
  1. Install VM Player 7


  1. Map Windows Server 2012 R2 ISO file to drive using Virtual Clone Drive

  1. Provide personalized login name and password to login to OS.

  1. Select the location in the local drive where you want to use the virtual machine.
  1. Specify Disk Capacity. The minimum recommended is 60GB.

  1. Click on Customize Hardware… and set the RAM for this VM. The minimum recommended is 12 GB for good performance.

  1. Set the Display for VM based on your guest machine resolution. Also set other required hardware settings.

  1. Click on Finish to start the OS installation in VM

  1. Windows Server 2012 setup is starting to install….

  1. OS Installation is complete.

Install SharePoint 2016 IT Preview

  1. Mount SharePoint 2016 ISO to a drive and install.
  1. The setup returned error
  1. The above error was because the prerequisites were not installed. So ran prerequisiteinstaller.exe from the setup path


  1. Error occurred during prerequisite installation
  1. Above error occurred while configuring Web Server (IIS) role. Also install WCF services and Web Server Role Services
  1. After Adding required features and configuring the services, run the prerequisiteinstaller.exe again
  1. Prerequisite installer failed again as an error occurred again. Following is the error when trying to run the setup.exe
  1. Looks like there is some issue in installing products internet connection is required. So enabled the settings to allow using internet connection for VM Ware from my local machine.  I’ve selected NAT which gives a virtual machine access to network resources using the host computer's IP address.

  1. After the above settings, Prerequisite installation started in the VMWare player
  1. However the installation failed again with following error.
  1. Following is error message logged in the prerequisite installer log file that the prerequisiteinstaller.Arguments.txt is missing.

  1. Looks like there still more features need to be installed manually
References:



  1. Prerequisite installation is completed successfully and press finish to restart the machine.

  1. Prerequisite Installation is completed successfully. Start the Setup again to install SP 2016 and enter the product Key available at the Microsoft download
  1. Accept the terms and select the file location.

  1. Installation started

  1. Error occurred during setup.
  1. Following error occurred while closing the installation window. “Microsoft Setup Bootstrapper has stopped Working.”

  1. Noticed that the internet connection is not enabled for Virtual Machine, so enabled it.
  1. Installation completed successfully and Configuration Wizard started.
  1. Click Next
  1. Click Yes and Next

  1. Select Connect to an existing server farm for standalone connection and create a new server farm for farm account
  1. Error occurred while specifying Configuration Database settings.
  1. After referring to the TechNet articles and forum, it looks like server role parameter is required when joining a farm. As suggested by the articles, the new spconfiguration database has been created using following PowerShell command.
Reference:

New-SPConfigurationDatabase –DatabaseName SharePoint_Config –DatabaseServer WINDOWS2012VM –AdministrationContentDatabaseName SharePoint_Content –Passphrase (ConvertTo-SecureString Summer01 –AsPlaintext –Force) –FarmCredentials (Get-Credential) -localserverrole SingleServerFarm
  1. After executing the above commands, the installation proceeds.

  1. Specify Port number and authentication provider for the Web Application


  1. SharePoint installation successfully completed.

  1. A look at SharePoint 2016 Team site home page.