3

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?

  • Use Sp.Guid as suggested by Rob – Namwar Rizvi Nov 27 '15 at 21:06
5

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

  • So will this just retrieve the GUID of the app part instance? Also will it require an async task, i.e. ajax call, to get, or does it just come automatically with the app part? – omega Nov 27 '15 at 16:03
  • The code above will create a new Guid. It's like Guid.NewGuid in .NET. It is a synchronous call.. – Rob Windsor Nov 27 '15 at 16:18
  • But what if I use SP.Guid, doesn't that just retrieve the GUID? I don't want to create a new one, that would defeat the purpose I am doing. – omega Nov 27 '15 at 16:47
  • First off, SP.Guid is a class, it's not a value. Second, when you say "retrieve the GUID", what Guid are you referring to? – Rob Windsor Nov 27 '15 at 16:58
  • I just wanted to get something that's unique to the instance of the app part from javascript without making an ajax call. For lists, they have a GUID like %7B29A614E5%2D68F1%2D45A2%2D85EC%2DCA18CBAE10F1%7D which would be the most ideal to get. Also for example, if I visit a page, I can get a lot of context information from javascript without an ajax call such as _spPageContextInfo. Basically I want something like that that gives me some unique ID for the app part instance. – omega Nov 27 '15 at 17:14

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.