4

I have the scenario where I have to create panel that contains list of all app parts (SharePoint-Hosted app) added to the SharePoint 2013 host pages, but within the SharePoint-Hosted app itself meaning that I have to use client side code (javascript). I can get host web page url (trought client script - document.referer) where the app part (ClientWebPart) is added. However I also need the ClientWebPart ID so I can verify (trough the LimitedWebPartManager) that this ClientWebPart exists on the host web page.

Does anyone knows how to get the ClientWebPart ID upon adding the sharepoint 2013 app part (ClientWebPart) on the SharePoint 2013 host web page by using client script (javascript) since I have to get this for my list located in the SharePoint-Hosted app?

Please do not get confused with the addional sharepoint app tokens _WPID_ and _WPQ_ which appear to be different things than the ClientWebPart ID that can be retrieved trough the LimitedWebPartManager.

4

I have found solution. The limitedWebPartManager.get_webParts() collection object has method called getByControlId(). Unfortunately, the MSDN reference of this (http://msdn.microsoft.com/en-us/library/jj245374.aspx) tells us nothing about it at the time I wrote this.

//JavaScript
var siteUrl = 'http://test.contosoapps.com/';  //the standart token SPHostUrl
            var hostPage = '/SitePages/DevHome.aspx';  //the document.referrer (JavaScript)
            var webPartId = 'g_b8b45300_2d7e_4319_bdf3_0379ac8802f8'; //the _WPID_ (SharePoint additional app part token)
            var webPartObject = null;
            function retrieveWebPartObject() {
                var clientContext = new SP.ClientContext(siteUrl);
                var file = clientContext.get_web().getFileByServerRelativeUrl(hostPage);
                var limitedWebPartManager = file.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
                webPartObject = limitedWebPartManager.get_webParts().getByControlId(webPartId);
                clientContext.load(webPartObject);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            }
            function onQuerySucceeded(sender, args) {
                if (webPartObject != null) {
                    alert('The app part exists!');
                }
            }
            function onQueryFailed(sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }

More detailed explanation on my blog: http://velin-georgiev-blog.appspot.com/blog/how-to-prove-sharepoint-app-partclientwebpart-wpid-existence-on-host-page-from-the-sharepoint-hosted-app-with-javascript-getbycontrolid/

  • How do you retrieve the web part ID from the app? – wjervis Jul 11 '14 at 17:11

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.