Search code examples
c#sharepoint-2010web-partscontent-editorwebpartpage

Is this a sensible way to programatically replace content on a SharePoint Page?


I have broken up a humongous page into several "bite-size" chunks. I want to present them, one at a time, to the user. After one page, they click an HTML button ("Next") and the previous Web Part or Content Editor Web Part is replaced by the next one.

From this page, I came up with some pseudcode (I don't know if there really is a SPLimitedWebPartManager.RemoveWebPart() method that I can call, as pseudocoded below, or how to get a reference to the currently (about-to-be-replaced) [Content Editor] Web Part, but this is the basic idea:

using (SPSite site = new SPSite("http://~"))
{
    SPWeb web = site.RootWeb;
    SPFile page = web.GetFile("Pages/Web_Form_Post_Travel_Expense.aspx");
    page.CheckOut();

    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        XmlElement d = new XmlDocument().CreateElement("div");
        d.InnerText = get
        ContentEditorWebPart cewp = new ContentEditorWebPart
        {
            Content = d
        };
        ContentEditorWebPart currentCEWP = ?;
        wpmgr.RemoveWebPart(currentCEWP);
        wpmgr.AddWebPart(cewp, "Header", 0);
    }

    page.CheckIn(String.Empty);
}

Does this make sense? It seems kludgy or even downright weird to me, to be checking out the page, changing it, and checking it back in.

The only other thing I can think of at the moment is to put the entire shebang into one Web Part, but keep sections 2-6 hidden at first, and then successively show one segment (after hiding the previous one). IOW, this type of procedure:

First Section shows
All others hidden

User selects "Next" button

First section is hidden
Second section shows, while all others remain hidden.

etc.

Actually, both ideas seem at least a little wonky to me. Is one approach preferable, or is there a third way I haven't thought of that would be better than both?


Solution

  • What are CheckOut() and CheckIn(string value) for? Is that the question?

    I looked at the Microsoft.SharePoint namespace and the SPSite class, but nothing appeared to be defined.

    My best guess is this is a way of placing a lock on a document while it is opened by another person.

    See the following supporting items:

    That way, if you have Mark Twain checked out to edit, no one else can check that out to edit.

    If, on the other hand, you simply want to view the contents, you could check out a section, read it all in to a StringBuilder, check the contents back in to SharePoint, and then display the text as you need it.