SharePoint problems fixed – Code tricks – Part 1

Hi,

Wanted to post some code tricks regarding SharePoint. Tricks which I have had to do more than once and tricks that are not so common but hard and useful:

Retrieve available web templates from a web:

SPWebTemplateCollection wtc = topNewsSite.GetAvailableWebTemplates(1035);

Set web default page layout(when you create new pages):

PublishingSite pubSite = newPublishingSite(yourSiteCollection);

                        {

                            PublishingWeb pubWeb = PublishingWeb.IsPublishingWeb(yourPublishingWeb) ? PublishingWeb.GetPublishingWeb(yourPublishingWeb) : null;

                            if(pubWeb != null)

                            {

                                PageLayout pageLayout = pubSite.PageLayouts.FirstOrDefault( item => item.Name == “yourpagelayout.aspx”);

                                if(pageLayout != null)

                                {

                                    pubWeb.SetDefaultPageLayout(pageLayout, false);

                                    pubWeb.Update();

                                }

                            }

                        }

 

Copy(Add), Modify a webpart from one page to another with changes to webpart display template

// temp variable for a MS webpart which we will copy from one page to another page

                    Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart newsWP = null;

                    // Get the needed page from your site and get that pages web part manager

                    using (var wpm = topNewsSite.Lists[“Pages”].Items[0].File.GetLimitedWebPartManager(PersonalizationScope.Shared))

                    {

                        // Search for the right webpart(you could use LINQ instead of a foreach)

                        foreach (var wp in wpm.WebParts)

                        {

                            // Check the type of the webpart

                            if ((wp is Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart))

                            {

                                // Make sure it is a ContentBySearchWebPart

                                newsWP = wp as Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart;

                               

                                // Check that we have the right webpart

                                if (newsWP != null && newsWP.Title.Contains(“My News Webpart”))

                                {

                                    // Get the page you need where you want the web part to be copied

                                    SPListItem page = newWeb.Lists[“Pages”].Items[0];

                                    SPFile file = page.File;

                                    file.CheckOut();

                                    // Get the web part manager of the destination page where to copy the webpart

                                    using (var wpmNewsWeb = file.GetLimitedWebPartManager(PersonalizationScope.Shared))

                                    {

                                        // Change the web part display template for a listing with paging

                                        newsWP.RenderTemplateId = “~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Control_ListWithPaging.js”;

                                        // Add the web part to the page

                                        wpmNewsWeb.AddWebPart(newsWP, “CenterZone”, 0);

                                        // Make sure to call this to save the changes to the webpart

                                        wpmNewsWeb.SaveChanges(newsWP);

                                    }

                                    page.Update();

                                    file.CheckIn(“”);

                                    file.Publish(“”);

                                    file.Approve(“”);

                                }

                            }

                        }

                    }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s