0

I m using simple code to assign HTML content to PublishingPageContent field of my ASPX page in SharePoint Online:

pageItem.PublishingPageContent = "<img src=\"smiley.gif\">";

However, the resulting page is empty. Why it happens? How to put an image into page body through code? Thank you.

  • where is the image located? – Nadeem Yousuf Feb 7 '18 at 6:53
  • Inspect element to see if it's actually rendering it or not – Arsalan Adam Khatri Feb 7 '18 at 7:17
  • @Nadeem Yousuf, Thank you for comment. As the link suggests, the image is stored alongside with the page in the same SharePoint folder. – Boris Zinchenko Feb 7 '18 at 7:32
  • @ArsalanAdamKhatri , Thank you for comment. As written, the page is empty. No content is rendered at all. – Boris Zinchenko Feb 7 '18 at 7:33
  • Can you grab the full Url of the image and use that in src attribute and see if that works. – Nadeem Yousuf Feb 7 '18 at 8:06
1

Try using the below code in a simple console application first. Helps in isolating the issue :

List list = context.Web.Lists.GetByTitle("Pages");
context.Load(list);

var item = list.GetItemById(8);
context.Load(item);
context.ExecuteQuery();

item["PublishingPageContent"] = "<img src =\"/sites/test/PublishingImages/smiley.gif\" />";

item.Update();
context.ExecuteQuery();

Have modified it to use server-relative url of the image. Modify it as per your image location in SharePoint.

Also, have closed the image tag, but i dont think it going to affect end-result since its self-closing.

  • Thank you very much. Very valid idea to reference an image by server relative URL. CSOM is all based on these types of links. I did not think of it before. I will have a try now and comment on results. – Boris Zinchenko Feb 7 '18 at 8:58
  • Your incredible advise worked perfectly. You made my day. I ve already despaired to guess myself and thought that all content is cut off by XSS filter universally. You ve made my day. Sorry delayed reply. – Boris Zinchenko Feb 7 '18 at 16:56

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.