none
Edit a content editor webpart inside a webpart page using VB.NET RRS feed

  • Question

  • hello's,

     

    Am trying to get into a publishing page and get the content editor webparts inside that page, then update the content of these

    webparts,

    everything went well, except the Update Process.

     

    can anyone help me and tell me what is wrong with the "webPartCollection.SaveChanges(x.StorageKey)" line

    This line always returns "The file is not checked out.  You must first check out this document before making changes."

    although am checking out the page.

    here is the code

     

    Dim mySite As SPSite = New SPSite("Server_Name")

     

    Dim myWeb As SPWeb = mySite.OpenWeb("Site_URL")

    myWeb.AllowUnsafeUpdates = True

     

    Dim myItems As SPListItemCollection = myWeb.Lists("Pages").Items

    Dim myItem As SPListItem

    For Each myItem In myItems

    If (myItem.Name = "default.aspx") Then

    Dim webPartCollection As SPWebPartCollection = myItem.File.GetWebPartCollection(Storage.Shared)

    Dim myPage As PublishingPage = PublishingPage.GetPublishingPage(myItem)

    myPage.CheckOut()

    Dim x As Microsoft.SharePoint.WebPartPages.WebPart

    For Each x In webPartCollection

     

    If Not (CType(x, ContentEditorWebPart) Is Nothing) Then

    ' Create an XmlElement to hold the value of the Content property.

    Dim ceWebPart As New ContentEditorWebPart()

    ' Create an XmlElement to hold the value of the Content property.

    Dim xmlDoc As New XmlDocument()

    Dim xmlElement As XmlElement = xmlDoc.CreateElement("MyElement")

    xmlElement.InnerText = CType(x, ContentEditorWebPart).Content.InnerText.ToString()

    xmlElement.InnerText = xmlElement.InnerText.ToString.Replace("Find_String", "Replace_String")

    ' Set the Content property to the XmlElement.

    ceWebPart.Content = xmlElement

    CType(x, ContentEditorWebPart).Content = ceWebPart.Content

     

    webPartCollection.SaveChanges(x.StorageKey)

    End If

     

    Next

    myItem.Update()

    myPage.CheckIn("")

    If (myPage.ListItem.ParentList.EnableVersioning) Then

    myPage.ListItem.File.Publish("")

    End If

    If (myPage.ListItem.ParentList.EnableModeration) Then

    myPage.ListItem.File.Approve("")

    End If

     

     

    End If

    Next

     

    Tuesday, August 28, 2007 6:59 AM

Answers

  • Hey Frendz....

     

    Found the solution.....

     

    We need to use SPLimitedWebPartManager.....

     

    Just replace the above code inside if statement....

     

    if (myItem.Name.ToUpper() == "DEFAULT.ASPX")

    {

     

    SPLimitedWebPartManager webPartCollection = myItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

     

    PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);

    myPage.CheckIn("if checked out");

    myPage.CheckOut();

    int u = webPartCollection.WebParts.Count;

    for(int i=0;i<=u;i++)

    {

    Microsoft.SharePoint.WebPartPages.WebPart webpt = (Microsoft.SharePoint.WebPartPages.WebPart)webPartCollection.WebPartsIdea;

    if (webpt.GetType().ToString() == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")

    {

    //Create an XmlElement to hold the value of the Content property.

    ContentEditorWebPart ceWebPart = new ContentEditorWebPart();

    // Create an XmlElement to hold the value of the Content property.

    XmlDocument xmlDoc = new XmlDocument();

    XmlElement xmlElement = xmlDoc.CreateElement("Content");

    xmlElement.InnerText = ((ContentEditorWebPart)webpt).Content.InnerText.ToString();

    xmlElement.InnerText = xmlElement.InnerText.ToString().Replace("hotmail", "Yahoo");

    //Set the Content property to the XmlElement.

    ceWebPart.Content = xmlElement;

    ((ContentEditorWebPart)webpt).Content = ceWebPart.Content;

    webPartCollection.Web.AllowUnsafeUpdates = true;

    webPartCollection.SaveChanges(webpt);

    webPartCollection.Web.AllowUnsafeUpdates = false;

    }

    }

     

    myPage.Update();

    myPage.CheckIn("");

     

    if (myPage.ListItem.ParentList.EnableModeration)

    {

    myPage.ListItem.File.Approve("");

    }

    }

     

    Thatz it... It works

     

    Happy Coding Smile

    Sunday, January 6, 2008 5:55 PM

All replies

  • Hi,

     

    I was also trying to do the same in C#.

     

    tried

     

    webPartCollection.Web.AllowUnsafeUpdates = true;

    webPartCollection.SaveChanges(x.StorageKey);

    webPartCollection.Web.AllowUnsafeUpdates = false;

     

    &

     

    myPage.Update();

     

    still we are missing some thing... Please let me know if you have found the solution.

     

    Regards,

    Fauzi

    Sunday, January 6, 2008 5:23 PM
  • Hey Frendz....

     

    Found the solution.....

     

    We need to use SPLimitedWebPartManager.....

     

    Just replace the above code inside if statement....

     

    if (myItem.Name.ToUpper() == "DEFAULT.ASPX")

    {

     

    SPLimitedWebPartManager webPartCollection = myItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

     

    PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);

    myPage.CheckIn("if checked out");

    myPage.CheckOut();

    int u = webPartCollection.WebParts.Count;

    for(int i=0;i<=u;i++)

    {

    Microsoft.SharePoint.WebPartPages.WebPart webpt = (Microsoft.SharePoint.WebPartPages.WebPart)webPartCollection.WebPartsIdea;

    if (webpt.GetType().ToString() == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")

    {

    //Create an XmlElement to hold the value of the Content property.

    ContentEditorWebPart ceWebPart = new ContentEditorWebPart();

    // Create an XmlElement to hold the value of the Content property.

    XmlDocument xmlDoc = new XmlDocument();

    XmlElement xmlElement = xmlDoc.CreateElement("Content");

    xmlElement.InnerText = ((ContentEditorWebPart)webpt).Content.InnerText.ToString();

    xmlElement.InnerText = xmlElement.InnerText.ToString().Replace("hotmail", "Yahoo");

    //Set the Content property to the XmlElement.

    ceWebPart.Content = xmlElement;

    ((ContentEditorWebPart)webpt).Content = ceWebPart.Content;

    webPartCollection.Web.AllowUnsafeUpdates = true;

    webPartCollection.SaveChanges(webpt);

    webPartCollection.Web.AllowUnsafeUpdates = false;

    }

    }

     

    myPage.Update();

    myPage.CheckIn("");

     

    if (myPage.ListItem.ParentList.EnableModeration)

    {

    myPage.ListItem.File.Approve("");

    }

    }

     

    Thatz it... It works

     

    Happy Coding Smile

    Sunday, January 6, 2008 5:55 PM