4

I need to determine the type of a web part (eg. Script Editor, Search Results, etc.) on pages using JavaScript.

I started with REST and succeeded in getting the web part manager for a page and got the web part definitions (SP.WebParts.WebPartDefinition) as well. Unfortunately there is no type information, only Title, TitleUrl, ZoneIndex, Id etc. There is also no Properties property which I saw in some samples.

My request looks like this:

https://siteUrl/_api/web/getFileByServerRelativeUrl(fileUrl)/getLimitedWebPartManager(scope=1)/webparts?$expand=Webpart

I'm interested in SharePoint 2013 and 2016. I read about an ExportWebpart API which was introduced in 2016 for SP Online which seems to be missing at least on my on-prem test farm. If there are any means for on-prem I'd also like to know any patch level needed for the API to be available. But I could probably google this myself once I know the API.

So how do I get the type of a web part using JavaScript and REST (or JSOM - maybe there are more possibilities?) for SP on-prem?


More information

When exporting the web part manually in the browser UI I get the information I need - in two places of the exported web part XML:

Metadata of the exported web part (This is what I'd like to have!):

<metaData>
  <type name="Microsoft.SharePoint.WebPartPages.ClientWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>

As an alternative (workaround) the properties would be (maybe) sufficient for my case as well. I could identify the web parts I'm interested in via their unique combination of properties like the FeatureId and others:

    <property name="FeatureId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">GUID I can use</property>

But I just cannot get these properties as well, using REST.

I checked that the export mode allows exporting the data:

enter image description here

That the web part properties are retrievable via Remote API:

enter image description here

I tried expanding WebPart.Properties instead of WebPart (like in some JSOM samples).

Nope. Maybe it's just not possible via REST? The documentation is no reason to party either.

  • Have you try "?contents=1" it will show you the web part types on the page – YogaPanda Dec 11 '17 at 14:48
1

I found a JS object called webpart properties... As alwas at JSON it's very "well" documented: https://msdn.microsoft.com/en-us/library/office/jj246541.aspx

Maybe the webpart type is hidden somewhere there... I haven't found it at the first glance, but here is the code, maybe a starting point:

var ctxOBJ = SP.ClientContext.get_current();
var webOBJ = ctxOBJ.get_web();
var siteOBJ = ctxOBJ.get_site();
var pageURL = window.location.pathname;
var webPartManagerOBJ = webOBJ.getFileByServerRelativeUrl(pageURL).getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartsOBJ = webPartManagerOBJ.get_webParts();
ctxOBJ.load(webPartsOBJ);
ctxOBJ.executeQueryAsync();

var webPartOBJ = webPartsOBJ.getById(0).get_webPart()
ctxOBJ.load(webPartOBJ);
ctxOBJ.executeQueryAsync(); 

var propsOBJ = webPartOBJ.get_properties()
ctxOBJ.load(propsOBJ);
ctxOBJ.executeQueryAsync(); 
  • Seems like the properties are only retrievable via JSOM. :-/ I could try identifying web parts by their set of properties. But only the type would be unique. Let's see if somebody knows something. Otherwise I have to go for JSOM+property poking. // A note about the code: this PoC is prone to timing issues and might fail. Copy-and-pasters beware ^^ – Heinrich Ulbricht Dec 8 '17 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.