Creating communication sites using Fiddler

Some SharePoint developers are still not familiar with how valuable Fiddler can be when developing applications. Some also would depend on other tools like Postman to get the job done, and others would find it hard to work with Fiddler and SharePoint at the same time​​ due to not being able to get authenticated to SharePoint from Fiddler. I’ll use this post to show the steps on how to use Fiddler with SharePoint online as well as show​​ the steps​​ to create communication sites using REST from Fiddler.​​ 

Obviously, first thing to do is to get Fiddler if you don’t have it already, you can get it​​ here.​​ The left pane in Fiddler is where network traffic is shown. Whenever you make a request or any program on your computer makes a request to any external server, Fiddler acts as a proxy and all traffic is passed through Fiddler as long as it’s running on your computer. You can notice that if you go to your internet options > Connections​​ tab >​​ Lan Settings, you’ll see something like this:​​ 

 

 

So now we know that Fiddler acts as a proxy and all network traffic is passed through it as long as it’s running. You can see all the traffic for your computer in the left pane in Fiddler.​​ On the right side, we have bunch of tabs, the only 2 we care about for now are the​​ Inspector​​ and​​ Composer​​ tabs.​​ Double-clicking on any​​ request will open the Inspector tab for you to see the Request (the top area), and the response (the bottom area). We’ll use the inspector tab first to get the cookies used to authenticate to our SharePoint online, later when we get the information about our cookies, we’ll use the Composer tab to make a request to post data to SharePoint to create a new communication site.​​ 

 

Let’s now get the cookies that are used to authenticate to SharePoint. Make sure you’re tracking capturing traffic​​ (remember all traffic info will appear in the left pane). To​​ verify that, click on the File tab and make sure that “Capture Traffic” is checked:​​ 

 

 

 

Next, open a SharePoint site in your Office 365 tenant, you’ll notice that Fiddler is going to capture that request:​​ 

 

 

Double-Click on that entry in Fiddler, and go to the Response section (the bottom right section in Fiddler) and click on the Headers tab:​​ 

 

 

You’ll notice​​ you have 2 cookies,​​ FedAuth, and​​ rtFa. Right click on the first one, and select​​ copy value only:​​ 

 

 

Open notepad, type​​ Cookies:​​ then paste what you copied, so you should have something like:​​ 

 

Cookies:​​ {CopiedValue}

Copy the second cookie value in Fiddler, then​​ in notepad​​ write​​ ;​​ afterwards​​ and paste the second value, so the full string you should​​ have​​ is:​​ 

 

Cookies: {FirstCopiedValue};{SecondCopiedValue}

Copy the whole text in notepad, and open the Composer tab in Fiddler, you’ll find it in the top right section. Change the HTTP request type to​​ POST​​ and paste the URL of your site in the next field and append​​ /_api/contextinfo,​​ then paste the string you had in notepad inside the Headers text area, so the composer tab should look like this:​​ 

 

 

Remember that the Cookie: … is the text you have as a result of what we did in notepad earlier when we captured the first request to our site. Again, it should be like this: Cookies: {firstCookieValue};{secondCookieValue}

 

Now click on​​ Execute, you’ll see that Fiddler captured the request in the list of requests on the left side.​​ Dobule-click on the request entry, you’ll see in the bottom right side (the response area)​​ in the​​ XML​​ tab there’s an entry for FormDigestValue:​​ 

 

Right click on this value and select​​ Copy, and head back to the Composer tab​​ where you were working, below the Cookies entry add this:​​ 

X-RequestDigest: {ValueofRequestDigest}​​ 

Change the URL to {YourSite}/_api/sitepages/communicationsite/create

So far you should have something like this:​​ 

 

 

So far so good! Now we have the Request Digest, we can make Post requests to SharePoint (Remember, you can make Get requests to SharePoint using only the Cookie entry without having to use X-RequestDigest. We only use X-RequestDigest to POST new data to SharePoint).​​ 

So, we got the Request Digest, what data do we need to POST to SharePoint? As we changed the URL to /_api/sitepages/communicationsite/create , that’s the end point to create new communication sites, so we’ll need to provide the info for this site in the request body (the bottom text area in Fiddler as shown in the previous image). Type this:​​ 

 

{
    
"request":{
      ​​ 
"__metadata":{
          
"type":"SP.Publishing.CommunicationSiteCreationRequest"
       },

      ​​ 
"Description": "Site created from Fiddler",​​ 
      ​​ 
"Title":"Communcation from Fiddler",
      ​​ 
"Url":"https://yourtenant/sites/CommFromFiddler"
    }

}​​ 

Try hitting Execute now, you’ll see an error in Fiddler on the left side:​​ 

It will tell you that you need to add a Content-Type in the header which we didn’t do, let’s add that, so go back to the Composer tab, and add the content type header as:​​ 

Content-Type: application/json;odata=verbose

 

 

Now hit​​ Execute​​ and you’ll see that you’ll get an entry in the requests area in the left pane with a result of 200, which means that the request was successful, if you navigate to the site specified in the Url in the request body, you’ll see that it’s created and it’s based on the Topic site design.​​ 

Note if you need to create an empty communication site or if you want to create one based on the showcase site design, you’ll need to add the SiteDesignId to the request body like this:​​ 

 

{
    
"request": {
        
"__metadata": {
            
"type": "SP.Publishing.CommunicationSiteCreationRequest"
        },

        
"Description": "Site created from Fiddler",
        
"Title": "Communcation from Fiddler",
        
"Url": "https://yourtenant/sites/CommFromFiddler",
        
"SiteDesignId": "f6cc5403-0d63-442e-96c0-285923709ffc"
    }

}

The above snippet will create an empty communication site. Showcase site design ID =​​ 6142d2a0-63a5-4ba0-aede-d9fefca2c767 .. and omitting the SiteDesignId will create a topic communication site.​​ 

 

An error you might face making a request to SharePoint is:​​ 

The type of data at position 5 is different than the one expected.

 

If you see this, make sure the request body you have is surrounded by curly braces { } not starting right away with​​ “request” : ..

 

I hope this was useful for you and you learned something new! Happy SharePointing!​​ 

Leave a Reply

Your email address will not be published. Required fields are marked *