0

I am trying to get a webpart from the gallary to add to a webpart page. It fails on the GetFile line saying that it can't find the file but the file is in the gallary. Is the code/path correct?

 SPFile myWebPart = newSite.RootWeb.GetFile(newSite.RootWeb.Url + "/_catalogs/wp/UserQuestionnaireSummaryWebPart.webpart");
    XmlTextReader read = new XmlTextReader(myWebPart.OpenBinaryStream());
     var wp = manager.ImportWebPart(read, out errMsg);
     manager.AddWebPart(wp, null, 1);
     manager.SaveChanges(wp);
  • Is this the default Questionnaire Summary web part or something custom? That kind of error usually indicates corruption or bad code within the web part itself. Any errors would probably be within the header code, since any problems with the XSLT content would let the part be added to a page, then generate an error on the page instead of the web part content. – Omegacron Mar 25 '15 at 20:51
  • No that is just the name of a webpart. The error is "can't find the file" – Kyle Johnson Mar 25 '15 at 21:31
  • I think I know why it can't find the file. It just dawned on me. The code above this creates a new site and web. The web part is in the parent site web part gallery. Could that be it? – Kyle Johnson Mar 25 '15 at 21:34
  • The actual web part tag (what I'm calling the header) is what tells SharePoint that the file is a web part. If it's messed up, SharePoint won't even add it to the page. As for the second question, the address is your code is relative to the root of the current site collection. If your current context is moved into a lower site collection, that could indeed cause the issue you're seeing (since the *.webpart file wouldn't exist in the new site collection's gallery, only the parent's). – Omegacron Mar 25 '15 at 21:38
1

The code above is correct. The webpart file name was wrong in my case.

  • Great job resolving your own error! :) – Quinn Johns Mar 26 '15 at 0:50

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.