Wednesday, June 6, 2018

Posting updates to SharePoint Online via PowerShell and Rest API

In my previous blog office 365 powershell and restApi i demonstrated how to read/get data from SharePoint online site, here in this example we will Post updates.

Here are the important things to consider when posting updates to office 365:-

  1. We have to pass  X-RequestDigest value for the form digest HTTP header.
  2. If updating Lists/Document Library and items/documents to avoid concurrency conflicts we need to specify additional HTTP header with the name "IF-MATCH", which takes a value of Etag. Etag can be retrieved by retrieving the target entity(list or list item) with a GET method. This is included in the response HTTP headers and in the response content. In situations where we dont care about concurrency its ok to pass "*" as value.
I will use the PnPOnline msi PowerShell module to connect to Office 365 developer tenant of mine. Kindly read the office 365 powershell and restApi to understand how to use it and load the module.

Lets talk about the script here.

  • First we will Import the PowerShell module.

Import-Module "Microsoft.Online.SharePoint.PowerShell"

  • Next create a global webSession variable, we will add headers to this session via different functions as our code progresses.

#create webRequest session global Object
$Global:webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession


  • Lets create a function where we will pass the url of the SharePoint online site as a param and then will connect to it using Connect-PnPOnline function of PnP SharePoint Online Powershell:-



function Init-PnPSecuritySession{
param($targetSite)
$targetSiteUri = [System.Uri]$targetSite
#connect to the Sharepoint online site 
Connect-PnPOnline $targetSite
$context = (Get-PnPWeb).Context
$credentials = $context.Credentials
$authenticationCookies = $credentials.GetAuthenticationCookie($targetSiteUri, $true);

#set retrieved cookies as header to wenreq
$Global:webSession.Cookies.SetCookies($targetSiteUri,$authenticationCookies)
$Global:webSession.Headers.Add("Accept","application/json;odata=verbose");

}
Will retrieve the Context from PnP web and then grab the credentials and Authentication Cookie.
Next setup the authentication cookie value and include Accept header for content type.


We are using odata=verbose, instead we can use odata = minimalmetadata or odata = nometadata for reducing metadata returned after the rest call.


  • Next we will write a function in which we will make a rest call to ContextInfo namespace, retrieve the RequestDigest value and pass it to websession object:-
#function to get RequestDigest Value and set it with Http Header
function Init-PnPDigestValue{
param ($targetSite)

$contextInfoUrl = $targetSite + "_api/ContextInfo"
$webRequest = Invoke-WebRequest -Uri $contextInfoUrl
-Method Post -WebSession $Global:webSession

$jsonContextInfo = $webRequest.Content | ConvertFrom-Json
$digestValue = $jsonContextInfo.d.GetContextWebInformation.FormDigestValue
$Global:webSession.Headers.Add("X-RequestDigest",$digestValue)
}


  • Now lets add additional HTTP Headers, as its an Update we will user X-HTTP-Method "MERGE" or You can use "PATCH"  along with Post so that any writable property that is not specified in the content Metadata we are passing, will retain its current value.

$Global:webSession.Headers.Add("X-HTTP-Method","MERGE")
$Global:webSession.Headers.Add("IF-MATCH", "*")#optional
$Global:webSession.Headers.Add("content-type","application/json;odata=verbose")

We are keeping If-Match to *, this is optional and one can ignore it in this case. Its important to mention content-type.

  • Now lets declare the content, we will use stringified JSON object to update Title of the Web here. so it should be like this:-
$newContent = "{ '__metadata': { 'type': 'SP.Web' }, 'Title': 'PHLY CITY Site' }";
$Global:webSession.Headers.Add("content-length",$newContent.Length)

Our Object is SPweb and the property we are modifying is Title. so newContent is set accordingly and also an additional Header parameter is added to provide the content-length.

Now lets provide our url for sharepoint site and call the functions and then finally Invoke the web request:-

$targetSite = "https://phly.sharepoint.com/sites/phly/"
Init-PnPSecuritySession -targetSite $targetSite
Init-PnPDigestValue -targetSite $targetSite
$restCallUrl = $targetSite + "_api/web"

#Invoking the web request with websession and POST method.
$webRequest = Invoke-WebRequest -Uri $restCallUrl -Body $newContent  
-Method Post -WebSession $Global:webSession 


Save the entire script and execute it, it will ask you credentials to login to office 365 site and then will invoke the web request. you should be able to see the updated result on the site:-




Wednesday, May 16, 2018

Get Projects Data using Project Server Rest API and PowerShell. Project Server 2013

Hi, One of the requirements my team received was to extract project server related information to automate Project Servers permission for which we need to extract project server reports on daily basis.
Its very easy and simple to use Project Server Rest API with PowerShell to extract Projects information.
It involves three basic steps:-


  1. Invoke-Webrequest to Project server rest API endpoint.
  2. Parse the result into JSON.
  3. filter the Json result and output to excel.
Here is the script to perform this activity

$webRequest = Invoke-WebRequest -Uri "http://{site url}/pwa/_api/ProjectData/Projects"
 -Headers @{"Accept"="application/json;odata=verbose"} -UseDefaultCredentials

$jsonData = $webRequest.Content | ConvertFrom-Json

$jsonData.d[0].results | Select ProjectTitle, ProjectOwnerName, ProjectId
 |Export-Csv -Path c:\ProjectServerDataFinal.csv       
 

And this will generate the csv file with desired input, you can get all these properties of project using basic Rest API uri and can also provide filters to as desired.

Wednesday, November 15, 2017

How to create File Share on Azure

Hi, i am working with azure these days, and the first thing i did was to create my own file share on the cloud/accessible via instead of using one drive or google drive.
So here are very easy steps to create your own cloud based shared drive on the Azure:-





  1. Login to azure portal:- https://portal.azure.com
  2. Click New and next search the market place for storage account (Use if you have any existing one)
  3. Click Create and keep settings as shown in figure below, the most important thing among settings is performance, for file share the performance have to be standard as Files storage is not available in Premium performance

  4. Wait for it to be created, next once created open the storage account and click on overview, next click on the Files in the adjacent window to open file service and click + to add a new file share:-

    One thing to note here that we can have file share size upto 5 TB i.e 5120 GB only
  5. Next click ok and we are good. to access it just open the file storage and click on Connect. You can choose the connection guidelines as per your wish, i prefer using "new use" command. Just copy the new use command and run command shell on your machine and paste the copied string and hit enter.

    Basically the string you copied is "net use [File Share Path] [User Account] [Access Key1 of Storage Account]".
  6. Just to add here, lets see the cost of creating 1 TB File Share Per Month on Pricing Calculator:-
  7. 62 Dollars plus operations cost.

Tuesday, October 10, 2017

SharePoint Online, PowerShell and Rest API : Introduction


In this blog i will show you how to execute Rest API calls via PowerShell for SharePoint Online(Office 365 Tenant).

I am using SharePoint Online PnP MSI here.

You must have already heard about PowerShell MSI for SharePoint Online developed by Erwin van Hunen and included in SharePoint PnP, i will use the same for connecting to SharePoint online via Powershell, you can download the same using this url:-

https://github.com/SharePoint/PnP-PowerShell/releases

Note:- You will need to have Powershell 3.0 to use SharePoint Online msi of SharePoint PnP.

to View all Commands in PnP, type :-

Get-Command -Module *PnP*

Here is the URL for help documentation for commands:-

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/readme.md


Once you have installed it, lets see how to execute Rest API calls via Powershell.

You can use Windows Powershell ISE, SharePoint Online Management Shell, CMDER

I am using CMDER to demo this:-

Open CMDER and type Powershell, and then load modules:-

If you are using cmder first time and would like to work using it with PowerShell Online, then import the module like this:-












Now we are ready to write our Command to talk to Rest API.

Follow these simple steps:-

  1. Connect to SP Online:-

     
    $email = Read-Host -Prompt "Please enter your tenant account email"
    $pass = Read-host -AsSecureString "Please enter tenant admin password"
    $credentials = New-Object –TypeName "System.Management.Automation.PSCredential"
     –ArgumentList $email, $pass
    $targetSite = "https://.sharepoint.com/sites//"
    $targetSiteUri = [System.Uri]$targetSite
    Connect-PnPOnline $targetSiteUri -Credentials $credentials
    
    
    
  2. Next retrieve the context from the connection:-

     
     $context = (Get-PnPWeb).Context
    
  3. Grab related authentication cookies

     
    #this is different object then other credentials used earlier
    $spcredentials = $context.Credentials
    $authenticationCookies = $spcredentials.GetAuthenticationCookie($targetSiteUri,
    $true)
    
    
  4. Initiate a web session and set Cookies and accept headers to it

    $webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $webSession.Cookies.SetCookies($targetSiteUri, $authenticationCookies)
    $webSession.Headers.Add("Accept", "application/json;odata=verbose")
    
    
  5. Rest call constructs, here we are just retrieving data of a library with a normal get request.

    $targetLibrary = "Documents"
    $apiUrl = "$targetSite" + "/_api/web/lists/getByTitle('$targetLibrary')"
    
  6. Invoke the request

    $webRequest = Invoke-WebRequest -Uri $apiUrl -Method Get -WebSession $webSession
    
  7. Get the results

    $jsonLibrary = $webRequest.Content | ConvertFrom-Json
    
This is how it will appear in the shell:-







we have successfully executed our Rest Call to SharePoint online.

To view the results you can consider d as an object and execute Select:-



Entire PS Script:-

$email = Read-Host -Prompt "Please enter your tenant account email"
$pass = Read-host -AsSecureString "Please enter tenant admin password"
$credentials = New-Object –TypeName "System.Management.Automation.PSCredential"
 –ArgumentList $email, $pass

$targetSite = "https://.sharepoint.com/sites//"
$targetSiteUri = [System.Uri]$targetSite
Connect-PnPOnline $targetSiteUri -Credentials $credentials
$context = (Get-PnPWeb).Context
#this is different object then other credentials used earlier
$spcredentials = $context.Credentials
$authenticationCookies = $spcredentials.GetAuthenticationCookie($targetSiteUri,
$true)

$webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$webSession.Cookies.SetCookies($targetSiteUri, $authenticationCookies)
$webSession.Headers.Add("Accept", "application/json;odata=verbose")
$targetLibrary = "Documents"
$apiUrl = "$targetSite" + "/_api/web/lists/getByTitle('$targetLibrary')"
$webRequest = Invoke-WebRequest -Uri $apiUrl -Method Get -WebSession $webSession
$jsonLibrary = $webRequest.Content | ConvertFrom-Json
$jsonLibrary.d | Select *

Friday, July 17, 2015

Javascript Prototypes and Prototype Pattern

JAVASCRIPT PROTOTYPES

Reason why i am blogging about this because i seriously like to share how i got my confusion removed regarding JavaScript Prototypes, i used prototypes for last 3 years just because someone has used it and with a confusion regarding its basics i was just implementing it, this is the case with many of us and this is one of the most confusing topic until unless you don't explore it practically.

I will show you how i actually got clear about prototype and finally i love these and consider its pattern as the most handy and important when comes to developing APPS.

Note:- I am using Chrome Developer tools console tab for demonstrations.

Prototype, as per the oxford dictionary a prototype is  :-

A first, typical or preliminary model of something, especially a machine, from which other forms are developed or copied.

So in simple words prototype is a model to create copies out of, and if i relate it at this point with JavaScript and my programming knowledge so it must be my functional blueprints to create objects out of, which is partially true but it has more functionality, now lets see what popular article says about prototype as single lines definitions:-
  1. A prototype is an object from which other objects inherit properties.
  2. Every JavaScript object has a prototype. The prototype is also an object.
  3. All JavaScript objects inherit their properties and methods from their prototype.
So keeping above three definitions in mind i thought to explore it Chrome browser console and see the practicality of definitions. But first for folks  who does not know what a JavaScript object is!
 here is a quick information:-

JavaScript Object (Layman Language {key:value} )
A JavaScript object is really just an unordered collection of key:value pairs, and you create  key:value pairs separated by commas and packed in Mustaches(Curly Braces):-

var Student = {Name:'John Doe', ID:1} // this is a container with properties.


A JavaScript object contains properties as shown above and can also contain methods/functions too :-

var Student = {Name:'John Doe', ID:1,SayHi:function(){alert('hey By ' + this.Name)}}

And to access property one has to use:- Student.Name or Student['Name']

To access methods:-
Student.SayHi();

Analyze JavaScript Object in Chrome console window for prototype

Open Chrome, default will be google.com, and just press F12 open console (Open Console window, i have put it in the source tab)
  1. There are many ways to create a simple blank object:-

  2. As you can see above i created ankur1,ankur2 and ankur object by different ways.
  3. Now when i checked my object ankur i can see some method which says ankur.isPrototypeOf () which raised my eye brows and immediately i thought using this method i can actually verify and relate the second definition which says every Object has a prototype.
Now if every object has prototype which is an object then there ankur.prototype should be that property/object.
Thinking the same when i checked in my browser console i got surprised to see that ankur.prototype is undefined:-

so is the definition wrong or confusing? Actually it turned out that it was  confusing not wrong:-

The true prototype of an object is held by the internal [[Prototype]] property. ECMA 5 introduces the standard accessor Object.getPrototypeOf(object) which to-date is implemented in Firefox, Safari, Chrome and IE9. In addition all browsers except IE support the non-standard accessor __proto__


And every object is created using with a Constructor initiation so we can access prototype object using prototype property of constructor too(this works in every browser):-


Now this gave me a bigger clarification on the definition of prototype:-

Every JavaScript object has a prototype. The prototype is also an object.

 Lets move forward and analyze the use of prototypes with its two other significant definition:-

  • A prototype is an object from which other objects inherit properties.
  • All JavaScript objects inherit their properties and methods from their prototype.


To show exactly what these two definition mean, we need first to know something very unique to functions in javscript:-

Every function in javscript has a property called prototype, and we use functions to create objects like i did above when used function Object() with new to create ankur1, just see this :-


Now as shown in the console above, we created a new function Myfunction and it does have a prototype Object which is the same when we create a new object as "new Myfunction()" and try to get the Prototype of that Object.

This prototype property does have significance when  functions are used as constructors to construct a Javascript Object.
A function’s prototype property is the object that will be assigned as the prototype to all instances created when this function is used as a constructor.

There is chain in javascript Objects, every Objects inherits from Object.prototype which sits at the top of the Chain.

Moving forward, i saw many examples on net where people have defined one level of inheritance using Protoypes, you can find multiple blogs which talks about single level of inheritance like this example:-



var humanConstructor = function human(name) { this.Name = name };
var Male = new humanConstructor("Man");
var Female = new humanConstructor("woman");
//later on i added this..
humanConstructor.prototype.From = function () {
    this.Name === "Man" ? alert("I am from Mars") : alert("I am from Venus");
}

Male.From();//Output:"I am from Mars"
Female.From();//Output: "I am from Venus"

The above example is very nice and simple one level inheritance example of prototypes definition mentioned above.
I just added From later on and automatically it got inherited by my objects.

Using this way i can customize my initial functions(constructors) and add Methods to prototype Object later on, Automatically all my Objects constructed using initial function will get new methods.
But it stores the reference of prototype not the value.


Till this it was fine, but later i thought:-
How can i create a Object from Male(from above example)  example Boy, and test multilevel inheritance and Chain behavior. And if i try to do this :-


var Boy = new Male(); //This will give error.

I got error:-

So, after a research i learned about Object.create()

So What does actually Object.create() do?

  • Object.create(proto [, propertiesObject ])

It extends a prototype and also can add properties using propertiesObject parameter.

So this is the way to create multilevel inheritance and define the chain mechanism in prototype.

See the use of Object.create():- here:-




var humanConstructor = function human(name) { this.Name = name };

var Male = new humanConstructor("Man");
var Female = new humanConstructor("woman");

//later on i added this..
humanConstructor.prototype.From = function () {
    this.Name === "Man" ? alert("I am from Mars") : alert("I am from Venus");
}

Male.From();
Female.From();
Male.About = "I am the man";
//Object.getPrototypeOf(Boy).About = "My Name is George, and i am  a Male";
var Boy = Object.create(Male);
alert(Boy.About);//I am the Man
alert(Female.About);//Unidentified
alert(Boy.Name);//Man
alert(Female.Name);//woman


One practical use of prototypes:-
This was where i recently used prototypes, i created a single page app that is meant to work on every browser, it was required to work even in IE8, there is a function in latest browsers called  "toISOString()", this was not working in IE8 and some other versions of browsers, but i did used it excessively, so to overcome the issue i used prototypes and created this :-



 if (!Date.prototype.toISOString) {
        (function () {
            function pad(number) {
                var r = String(number);
                //it takes data type in consideration...
                if (r.length === 1) {

                    r = '0' + r;
                }
                return r;

            }

            //here in this case of prototype pattern, this is the object/datatype on which prototype is applied.
            Date.prototype.toISOString = function () {

                return this.getUTCFullYear()
                   + '-' + pad(this.getUTCMonth() + 1)
                   + '-' + pad(this.getUTCDate())
                   + 'T' + pad(this.getUTCHours())
                   + ':' + pad(this.getUTCMinutes())
                   + ':' + pad(this.getUTCSeconds())
                   + '.' + String((this.getUTCMilliseconds() / 1000).toFixed(3)).slice(2, 5)
                   + 'Z';
            };
        }());
    }

Still there is enough left to discuss about JavaScript Prototype but in this blog i wanted to discuss the actual basics of prototypes with practical impressions. Hope it helps.

Tuesday, July 14, 2015

JavaScript Closures (Step 1 for App development)

JavaScript Closures

In this blog i like to write and discuss about the Closures in JavaScript, i am using JavaScript in SharePoint sites from 2007 version, In my first working assignment i was told to premeditate a Site template made using SPServices and Jquery to new SharePoint JavaScript client object model Site back in 2009, It was a Template which enables functionality of  Twitter on SharePoint. But that time i just created and written everything in functions which were added under Global namespace automatically.
And this is normally we do in creating our applications or making some modifications, but with the advancement in client side programming and trend of Single Page applications and many other factors  has introduced design patterns and writing javascript libraries.

To make such libraries one basic important concept is Closure.

A closure is concept made out of anonymous functions and a basic fact in javascript that an anonymous function can be returned from other functions, like it  can also be assigned to a variable

Local variables defined within the containing function are available through the returned anonymous function and this is called Closure.

Now lets see this in a series of examples:-


  1. Basic Closure example:-
    var Maddy = window.Maddy || {};
    
    Maddy.getStudentName = function Student(id) {
        var students = { '1': 'ankur', '2': 'nelson', '3': 'drew' };
        var getName = function () {
            alert(students[id]);
        };
        return getName;
    
    };
    
    Maddy.getStudentName(3)();
    


    here we have a namespace Maddy and under that we have a function getStudentName which incorporates an anonymous function accessing local variable students  and retrieving the value of name of student contained in students variable.

    We have just returned the function to the original function.
  2. The inner function is the closure in above example, it has three access chains, it can access its own variable and variables scoped to outer function and also can access Global function, but it can not access arguments directly of outer function, see this example:-

    Look at this example which does not use Closure:-

    Maddy.addArguments = function()
    {
        var sum =0;
        for(var i=0;i<arguments.length;i++)
        {
    
            sum += Number(arguments[i]);
        }
    
        return sum;
    }
    alert(Maddy.addArguments(1,2,3,4,5));
    
    

    It will work perfectly and will give you output of 15.

    Now if you use Closure and try to access arguments it wont work.

    Maddy.addArgumentsWithclosure = function () {
        var sum = 0;
    
        var doTheSum = function () {
            for (var i = 0; i < arguments.length; i++) {
    
                sum += Number(arguments[i]);
            }
    
            return sum;
        };
        return doTheSum;
    };
    
    alert(Maddy.addArguments(1,2,3,4,5)());
    

    it will return '0' in alert and wont work.

    So Closure can access local,outer and global variables but cant work with outer function arguments object.
  3. Closures have access to the outer function’s variable even after the outer function returns, also it stores references to the outer function’s variables;

    example demonstration:-

    Maddy.getValueID = function () {
        var valueId = 101;
    
    
        return {
    
            get_valueId: function () {
    
                return valueId;
            },
    
            set_valueId: function (newValue) {
                valueId = newValue;
            }
    
    
    
        };
    
    };
    
    var dummyVar = Maddy.getValueID();
    alert(dummyVar.get_valueId()); //output will be 101
    dummyVar.set_valueId(1000); 
    alert(dummyVar.get_valueId()); //output will be 1000
    
    
As shown above set_valueId closure still keeps reference to outer function variables even outer functions is allready executed.

Wednesday, July 1, 2015

Configure HostHeaders for SharePoint Web Application in Dev Boxes.

Hi,

How bad it looks when developing our applications in our dev boxes we use url's like http://xyz-servername:port/

With 2013 to configure apps we need to have some DNS entries created. But traditionally not every developer care to have dns entries created for web applications in dev boxes.

Also learning from my past lessons with different urls in Dev and Production for the simmilar sites, sometimes you have to manually change the urls in infopath forms, workflows to republish the working stuff.

To make things better, here is the way i am sharing on how to configure host header and edit bindings to let the host header work for you without configuring anything on DNS.

We will use Powershell to modify the host file and will also set bindings on IIS to make WebApplication availlable outside the server.

Here are the steps:-

  1. Create a WebApplication (You can extend one if you want to override the existing one).
    I just have created a normal web application with host header "ankurmadaan" and port i used is 80. Note:- i allready have a sharepoint default web application on port 80.
  2. Now we need a root site collection,  I like to keep my site collections in there own database, for me it will be root collection so it will go into default database.

  3. So now if you try to browse, you will be surprise to see that your site does not works, unless you have an entry for http://ankurmadaan in DNS mapped to ip of your server.
  4. Also you will be loop back and Authentication Required pop up will keep coming.
  5. So here is the solution, we will write Powershell Script to make this work:-
  6. First we will write script to get path of hosts file and create a copy of it:-
    #Make backup copy of the Hosts file with today's date 
    $hostsfile = 'C:\Windows\System32\drivers\etc\hosts'
    $date = Get-Date -UFormat "%y%m%d%H%M%S"
    $filecopy = $hostsfile + '.' + $date + '.copy'
    Copy-Item $hostsfile -Destination $filecopy
    
    
  7. Above code will create a copy of host file for safe way purpose.
  8. Next we will add entry for "ankurmadaan" in it:-
    Write-host "Adding entry for webapplication" 
    add-content -path $hostsfile -value "127.0.0.1 `t ankurmadaan"

  9. Last but not the least if not done allready in your server, entry for LoobBackCheck:-
    # Disable the loopback check
     New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword

  10. So here it is the working http://ankurmadaan/ :-
    
  11. If you have multiple webapplications and you want to do this entire step in one go, in that case you can use alternate access mappings, here is the script for it:-

    # Get a list of the AAMs and weed out the duplicates $hostsfile = 'C:\Windows\System32\drivers\etc\hosts' $hosts = Get-SPAlternateURL | ForEach-Object {$_.incomingurl.replace("https://","").replace("http://","")} | where-Object { $_.tostring() -notlike "*:*" } | Select-Object -Unique # Get the contents of the Hosts file $file = Get-Content $hostsfile $file = $file | Out-String # write the AAMs to the hosts file, unless they already exist. $hosts | ForEach-Object { if ($file.contains($_)) {Write-Host "Entry for $_ already exists. Skipping"} else {Write-host "Adding entry for $_" ; add-content -path $hostsfile -value "127.0.0.1 `t $_ " }}
  12. Now, this will work when you are inside the servers, what will you do to have it accessed by users from outside of the server in your network, like you want to demo it someone, so here is the way to do it:-
  13. Go to IIS  and right click on this Web Application and click on Edit Bindings:-

  14. Add a new entry and specify the port name and add server name(current server) on which you are configuring this, WFE if your dev box has multiple servers:-




  15. Click ok and the last step is to create an extra alternative access mapping. Open Central Admin ->Configure alternate access mappings -> Add Internal URLs -> Change alternate access mapping location to this web application.

  16. Add internal url http://servername:93 what we used in step 14. also select custom for zone
  17. Now browse http://servername:93 , it will open the same site as http://ankurmadaan will open in the server.


    NOTE:- you have to configure hosts file in each of the server connected to your dev box but binding can be done on any WFE to expose the site outside of the server.


Thanks.