Domanda

I am making an app part in SharePoint 2013, the one where I can insert into a page like a web part. In the javascript code, for my purpose, I need a unique identifier for the instance of the app part I added to the page. For example, the GUID of a list is globally unique, this would be ideal to get. Is there a way I can get this from the javascript code?

È stato utile?

Soluzione

There's an SP.Guid type in the JavaScript Client Object Model (in sp.runtime.js). To create a new Guid use SP.Guid.newGuid

UPDATE:

This update is based on further investigation done in response to the comments below.

Just like a "normal" web part, each instance of an app part has a web part id (which is unique). You can get the web part id in the code for the app part by following these steps.

Modify the Content element in the element manifest for the app part so that it adds the web part id to the query string. This can be done by adding &wpId=_WPID_ after {StandardTokens}.

<Content Type="html" Src="~appWebUrl/Pages/HelloWorldClientWebPart.aspx?{StandardTokens}&amp;wpId=_WPID_" />

Then add the code in your app part page to extract the web part id from the query string.

var id = "";
var params = document.URL.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
    var param = params[i].split("=");
    if (param[0] == "wpId") {
        id = decodeURIComponent(param[1]);
        break;
    }
}

The id should be "g_" followed by a Guid. (e.g. "g_4e04d031_0875_4bab_91ea_e13de4221ab8")

I found this information in this blog post on TechNet: HOW TO: Detect an App Part in edit mode in SharePoint 2013

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top