1

I am trying to add a webpart trough and app. I have provisioned some files in different extensions sucessfully.

The last step for my app is to add a content editor web part on the hostweb welcome page, now it says undefined. Heres my code:

"use strict";
//Provision Web Part on Host Web
var hostweb;
var hosturl;

function getRelativeUrlFromAbsolute(absoluteUrl) {
absoluteUrl = absoluteUrl.replace('https://', '');
var parts = absoluteUrl.split('/');
var relativeUrl = '/';
for (var i = 1; i < parts.length; i++) {

    relativeUrl += parts[i] + '/';
}
return relativeUrl;
}

function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == param) {
        return singleParam[1];
    }
 }
}

function addWebPart() {

var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var currentcontext = new SP.ClientContext.get_current();
var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
var hostweb = hostcontext.get_web();

//add a content editor web part to the default.aspx page
                                                                //Default.aspx
var pageToAddWP = hostweb.getFileByServerRelativeUrl(hostUrl +    "SitePages/DevHome.aspx");

//get out webpartmanager object for imports
var limitedWebPartManager = pageToAddWP.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);

var webPartXml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' +
    '<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"' + 
    ' xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"' + 
    ' xmlns=\"http://schemas.microsoft.com/WebPart/v2\">' + 
    '<Title>My Web Part</Title><FrameType>Default</FrameType>' + 
    '<Description>Use for formatted text, tables, and images.</Description>' + 
    '<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>' + 
    '<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>' + 
    '<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>' + 
    '<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>' + 
    '<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />' + 
    '<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />' + 
    '<MissingAssembly>Cannot import this Web Part.</MissingAssembly>' + 
    '<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />' + 
    '<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, ' + 
    'PublicKeyToken=94de0004b6e3fcc5</Assembly>' + 
    '<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>' + 
    '<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />' + 
    '<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">' + 
    '<![CDATA[This is a first paragraph!<DIV>&nbsp;</DIV>And this is a second paragraph.]]></Content>' + 
    '<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>';


var webPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
var webpart = webPartDefinition.get_webPart();

limitedWebPartManager.addWebPart(webpart, "Left", 1);

currentcontext.load(webpart);
currentcontext.executeQueryAsync(onAddWPSuccess, onAddWPFailed);
}
function onAddWPSuccess() {

 $('#message').append('<br /><div>WebPart succesfully added!</div>');
}
  function onAddWPFailed(sender, args){

$('#message').append('<br />Failed to provision webpart into host web. Error:' +   sender.statusCode);
 }
  • What says undefined? – wjervis Aug 15 '14 at 11:34
  • It says: Value does not fall within the expected range. – Anders Aug 16 '14 at 0:33
  • I have the same issue :( – jobin Feb 27 '15 at 18:53
1

Try passing the relative URL:

var pageToAddWP = hostweb.getFileByServerRelativeUrl(getRelativeUrlFromAbsolute(hostUrl) + "SitePages/DevHome.aspx");

Also, change the assembly version to 16 in your webPartXml code.

<Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  • I get this error: Value does not fall within the expected range. My code works when i not use it in a app – Anders Aug 16 '14 at 0:35
  • I fixed it, the right way was "/sites/mysitecollection/SitePages/home.aspx" – Anders Aug 16 '14 at 0:51

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.