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
Setup New Virtual Machine Wizard

  1. Provide personalized login name and password to login to OS.
Setup New Virtual Machine

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

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

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

Enable Shared Folders
  1. Click on Finish to start the OS installation in VM
Start the OS installation in VM

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

Installing Windows Server 2012
  1. OS Installation is complete.
Windows Server 2012 Installation complete

Install SharePoint 2016 IT Preview

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

Agree license terms

SharePoint 2016 IT Preview preparation tool
  1. Error occurred during prerequisite installation
Error occurred during prerequisite installation
  1. Above error occurred while configuring Web Server (IIS) role. Also install WCF services and Web Server Role Services
Error occurred during configuring Web Server role
Add Roles and Features
Add Roles and Features
Add Roles and Features
Add Roles and Features - confirmation
  1. After Adding required features and configuring the services, run the prerequisiteinstaller.exe again
Run pre-requisite installer
  1. Prerequisite installer failed again as an error occurred again. Following is the error when trying to run the setup.exe
pre-requisite installer error
  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.
Network Adapter settings in VM

Network Adapter settings in VM
  1. After the above settings, Prerequisite installation started in the VMWare player
Microsoft SharePoint 2016 Products Preparation Tool
  1. However the installation failed again with following error.
Error occurred in Microsoft SharePoint 2016 Products Preparation Tool
  1. Following is error message logged in the prerequisite installer log file that the prerequisiteinstaller.Arguments.txt is missing.
Missing prerequisite Installer Arguments text file

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

Add Roles and Features Wizard

Microsoft SharePoint 2016 Products Preparation Tool

Microsoft SharePoint 2016 Products Preparation Tool
  1. Prerequisite installation is completed successfully and press finish to restart the machine.
Pre-requisites Installation

SharePoint 2016 IT Preview Installation Complete
  1. Prerequisite Installation is completed successfully. Start the Setup again to install SP 2016 and enter the product Key available at the Microsoft download
SharePoint 2016 IT Preview Installation Complete SharePoint 2016 IT Preview Product Key
  1. Accept the terms and select the file location.
SharePoint 2016 IT Preview License TermsSharePoint 2016 IT Preview Choose File Location

  1. Installation started
SharePoint 2016 IT Preview Installation

  1. Error occurred during setup.
SharePoint 2016 IT Preview Installation Error
  1. Following error occurred while closing the installation window. “Microsoft Setup Bootstrapper has stopped Working.”
SharePoint 2016 IT Preview Installation Error Troubleshoot

  1. Noticed that the internet connection is not enabled for Virtual Machine, so enabled it.
Enable Internet Connection Virtual Machine
  1. Installation completed successfully and Configuration Wizard started.
SharePoint 2016 Run Configuration Wizard
  1. Click Next
SharePoint Products Configuration Wizard
  1. Click Yes and Next
SharePoint Products Configuration Wizard

  1. Select Connect to an existing server farm for standalone connection and create a new server farm for farm account
SharePoint Products Configuration Wizard
  1. Error occurred while specifying Configuration Database settings.
SharePoint Products Configuration Wizard 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
Configure Database
  1. After executing the above commands, the installation proceeds.

Modify server farm settings
  1. Specify Port number and authentication provider for the Web Application
Configure SharePoint Central Administration Web Application

Completing the SharePoint Products Configuration Wizard

Configuration Successful
  1. SharePoint installation successfully completed.
SharePoint installation successfully completed.

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