Skip to main content
edited body
Source Link
Alexander
  • 8.2k
  • 2
  • 28
  • 42

You can use SPLimitedWebPartManager to to do this. There is an example how to add Content Editor web part to the page, but in your case it should work too. If you works with published page you can use this code.

gWeb publishingWeb = PublishingWeb.GetPublishingWeb(webSite);
SPListItem item = publishingWeb.PagesList.GetItemByFileName(pageName);
if (item != null)
{
    PublishingPage publishingPage = PublishingPage.GetPublishingPage(item);
    try
    {
        if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        {
            publishingPage.CheckOut();                     
        }                  
        else
        {
            item.File.UndoCheckOut();
            publishingPage.CheckOut();
        }
     
        webPartManger = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

        //code that adds web part

        publishingPage.Update();
        publishingPage.CheckIn(updateComment);
     
        if (publishingWeb.PagesList.EnableModeration)
        {
            item.File.Publish(updateComment);
            item.File.Approve(updateComment);
        }
    }
    finally
    {
        if (webPartManager != null)
        {
            webPartManager.Dispose();
        }
    }
}

You can use SPLimitedWebPartManager to to this. There is an example how to add Content Editor web part to the page, but in your case it should work too. If you works with published page you can use this code.

gWeb publishingWeb = PublishingWeb.GetPublishingWeb(webSite);
SPListItem item = publishingWeb.PagesList.GetItemByFileName(pageName);
if (item != null)
{
    PublishingPage publishingPage = PublishingPage.GetPublishingPage(item);
    try
    {
        if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        {
            publishingPage.CheckOut();                     
        }                  
        else
        {
            item.File.UndoCheckOut();
            publishingPage.CheckOut();
        }
     
        webPartManger = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

        //code that adds web part

        publishingPage.Update();
        publishingPage.CheckIn(updateComment);
     
        if (publishingWeb.PagesList.EnableModeration)
        {
            item.File.Publish(updateComment);
            item.File.Approve(updateComment);
        }
    }
    finally
    {
        if (webPartManager != null)
        {
            webPartManager.Dispose();
        }
    }
}

You can use SPLimitedWebPartManager to do this. There is an example how to add Content Editor web part to the page, but in your case it should work too. If you works with published page you can use this code.

gWeb publishingWeb = PublishingWeb.GetPublishingWeb(webSite);
SPListItem item = publishingWeb.PagesList.GetItemByFileName(pageName);
if (item != null)
{
    PublishingPage publishingPage = PublishingPage.GetPublishingPage(item);
    try
    {
        if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        {
            publishingPage.CheckOut();                     
        }                  
        else
        {
            item.File.UndoCheckOut();
            publishingPage.CheckOut();
        }
     
        webPartManger = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

        //code that adds web part

        publishingPage.Update();
        publishingPage.CheckIn(updateComment);
     
        if (publishingWeb.PagesList.EnableModeration)
        {
            item.File.Publish(updateComment);
            item.File.Approve(updateComment);
        }
    }
    finally
    {
        if (webPartManager != null)
        {
            webPartManager.Dispose();
        }
    }
}
added 1248 characters in body
Source Link
Alexander
  • 8.2k
  • 2
  • 28
  • 42

You can use SPLimitedWebPartManager to to this. There is an example how to add Content Editor web part to the page, but in your case it should work too. If you works with published page you can use this code.

gWeb publishingWeb = PublishingWeb.GetPublishingWeb(webSite);
SPListItem item = publishingWeb.PagesList.GetItemByFileName(pageName);
if (item != null)
{
    PublishingPage publishingPage = PublishingPage.GetPublishingPage(item);
    try
    {
        if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        {
            publishingPage.CheckOut();                     
        }                  
        else
        {
            item.File.UndoCheckOut();
            publishingPage.CheckOut();
        }
     
        webPartManger = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

        //code that adds web part

        publishingPage.Update();
        publishingPage.CheckIn(updateComment);
     
        if (publishingWeb.PagesList.EnableModeration)
        {
            item.File.Publish(updateComment);
            item.File.Approve(updateComment);
        }
    }
    finally
    {
        if (webPartManager != null)
        {
            webPartManager.Dispose();
        }
    }
}

You can use SPLimitedWebPartManager to to this. There is an example how to add Content Editor web part to the page, but in your case it should work too.

You can use SPLimitedWebPartManager to to this. There is an example how to add Content Editor web part to the page, but in your case it should work too. If you works with published page you can use this code.

gWeb publishingWeb = PublishingWeb.GetPublishingWeb(webSite);
SPListItem item = publishingWeb.PagesList.GetItemByFileName(pageName);
if (item != null)
{
    PublishingPage publishingPage = PublishingPage.GetPublishingPage(item);
    try
    {
        if (item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        {
            publishingPage.CheckOut();                     
        }                  
        else
        {
            item.File.UndoCheckOut();
            publishingPage.CheckOut();
        }
     
        webPartManger = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

        //code that adds web part

        publishingPage.Update();
        publishingPage.CheckIn(updateComment);
     
        if (publishingWeb.PagesList.EnableModeration)
        {
            item.File.Publish(updateComment);
            item.File.Approve(updateComment);
        }
    }
    finally
    {
        if (webPartManager != null)
        {
            webPartManager.Dispose();
        }
    }
}
Source Link
Alexander
  • 8.2k
  • 2
  • 28
  • 42

You can use SPLimitedWebPartManager to to this. There is an example how to add Content Editor web part to the page, but in your case it should work too.