SharePoint Experts, Information Architects, Expert Witness

We provide consulting in a broad array of business and technology from architecture to design to deployment of global systems with a focus on surfacing data in the enterprise. Specialists in Microsoft, we are a premier provider of SharePoint Expertise (including 2016 and Office 365). We also provide Expert Witness/Legal Expert in eDiscovery, source discovery, patent infringement, piracy and more! We also have established SICG DLDS s.a. - our counterpart in Costa Rica that specializes in water systems (http://www.crwatersolutions.com) - Contact me direct: david_sterling@sterling-consulting.com or call 704-873-8846 x704.

Search This Blog

Saturday, April 15, 2017

Getting the Web ID for a site (SharePoint & Online)

There's often a need to find the Web ID of a SharePoint site - specifically when exporting web parts (as shown in this article https://chuvash.eu/2014/09/19/export-any-web-part-from-a-sharepoint-page/).

NOTE: I don't take credit for these, these are a summary of what's out there!

So getting the ID can be done a number of ways - first, using the "API" (if using SharePoint Online, authenticate to the site first):

https://<site>/_api/web/id

For Online:

https://<site>.SharePoint.com/_api/web/id

subsite:

https://<site>.sharepoint.com/sites/subsitecoll/_api/web/id

Next using on premise PowerShell:

$site = Get-SPSite  http://<site & port>
$web = $site.AllWebs["<name of site>"]
$web.ID

In SharePoint Online using the SharePoint Online Management Shell:

Connect-SPOService -URL https://<org>-admin.sharepoint.com

Enter your credentials, then enter:

Get-SPOSite -Identity https://<site>.SharePoint.com
Next connect to the site:

Connect-SPOSite -Url "https://<site>.sharepoint.com/" -Credential "<someone@org.com>"

If you omit -Credential, you will be prompted (the credential is your EMAIL address <someone@org.com>, not the AD account domain\account) but better to use this:

$username = "<someone@org.com>"
$password = "<password>"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(ConvertTo-SecureString $password -AsPlainText -Force)
Connect-SPOSite -Url "https://<site>.sharepoint.com/" -Credential $cred


Then finally for the top level web:

Get-SPOWeb -Identity "/"

For a subsite, use the name:

Get-SPOWeb -Identity "News"

Get-SPOWeb will list out details of the web - just look for the "Id" column.

Note - if using a Subsite under /Sites/, you have to connect to that site collection (instead of https://<site>.SharePoint.com, you use https://<site>.SharePoint.com/sites/subcollection".

Another way is through the URL. Open the site/web you want and open Site Settings. Click either the Site Content and Structure or Content and Structure Logs link. Click on the web in question and select the drop down then select General Settings - cut and paste the URL into notepad - you will find the ID in the query string after SPWeb (%3A = : - see here http://www.degraeve.com/reference/urlencoding.php). Here's snip of the URL where the SPWeb can be found:

FSmtContext%3DArea%3A%3FSPWeb%3A1b43b8ac-973e-4291-a268-fc9c0ecb4eb3%3A%26SmtContextExpanded%


Yet another is using jQuery - found on the MSDN site: 

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script><script type="text/javascript">
function GetSPWebID()
{
        url = document.getElementById("field1").value;
        var webId;
  
 var soapEnv =
    "<?xml version='1.0' encoding='utf-8'?>\
  <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\
    <soap:Body>\
    <GetWeb xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
    </GetWeb>\
    </soap:Body>\
    </soap:Envelope>";
  $.ajax({
      url: url + "/_vti_bin/SiteData.asmx",
      beforeSend: function(xhr) {xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetWeb");},
      type: "POST",
      data: soapEnv,
      dataType: "xml",
      async: false,
      complete: function(xData, status) {webId = $(xData.responseXML).find('WebID').text();},
      contentType: "text/xml; charset='utf-8'"
    });
 document.getElementById("field2").value=webId;
}</script>Root Web URL: <input id="field1"/><br/>WebID: <input id="field2"/> <br/><br/><button onclick="GetSPWebID()">Get Web ID</button> 
//nonsense comment to keep IE7 from truncating MSDN code

 

No comments: