Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Adding resuable HTML programmatically

  Asked By: Jay    Date: Jan 15    Category: Sharepoint    Views: 4106

I've been trying to add a reusable HTML fragment programmtically for some time now without success.

My plan was to insert a reusable HTML fragment using the default GUI, and insert that very same HTML into the HTML field of a ListItem in my code. However, even if I do this, the HTML that is saved in the field goes through a validation process, gets stripped of the attributes that are used to identify it.

So, basically I would like to know if there is some way to bypass the HTML validation, or if there is a function hidden somewhere in the API that can be used to insert reusable HTML fragments. I've been digging around in Reflector without much success, so I thought I'd throw this question up here before spending any more time on this issue.



1 Answer Found

Answer #1    Answered By: Yvonne Rodriquez     Answered On: Jan 15

Ok, so it seems like I'm doing the most of the posting in this thread myself. But then again, I started the thread, so I guess it is somewhat appropriate :>

Anyway, I've managed to find a solution. Nasty looking code  sample from my console application:

Code Snippet

PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
if (pWeb.DefaultPage.CheckOutStatus == SPFile.SPCheckOutStatus.None)
SPListItem item = pWeb.DefaultPage.Item;

item[FieldId.PublishingPageContent] = @"

<div id=""__publishingReusableFragmentIdSection"">

<a href=""/ReusableContent/2_.000"">a</a>



<span id=""__publishingReusableFragment""></span>

pWeb.DefaultPage.CheckIn("Hello world");

Basically, I just inserted a reusable  fragment using the HtmlEditor, looked up the item in the content database, and copied the format of the string.

The HTML itself is pretty straightforward. The only thing you need to worry about is this:

<a href=""/ReusableContent/2_.000"">a</a>

It is basically the URL of the list, + the ID of the item from which you want to insert  your reusable fargment.

Didn't find what you were looking for? Find more on Adding resuable HTML programmatically Or get search suggestion and latest updates.