SharePoint problems fixed – Code tricks – Part 1


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);






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;


                                    // 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












Good to Know: Windows Server 2012 – Part 4

Hi this is my part four of resources for Windows Server 2012(and older ones also):

Adding a Reverse Lookup Zone
Authorize a DHCP server in Active Directory
Block Inheritance
Configure a Server Core Server with Sconfig.cmd
Configure Memory and Processors
Delegate Control of an Organizational Unit
Deploy Clustered Storage Spaces 
Deploying a GlobalNames Zone
Deploying Microsoft RemoteFX on a Single Remote Desktop Virtualization Host Server Step-by-Step Guide
Deploying Network Load Balancing (NLB) and Virtual Machines on Windows Server 2008 R2
Deployment Image Servicing and Management Command-Line Options
Enforce a Group Policy Object Link
Evaluation Versions and Upgrade Options for Windows Server 2012
Evaluation Versions and Upgrade Options for Windows Server 2012
Grant a Member the Right to Logon Locally
How to remove data in Active Directory after an unsuccessful domain controller demotion
How User Account Control Works
Hyper-V Dynamic Memory Configuration Guide
Hyper-V Virtual Switch Explained, Part 2 
Hyper-V Virtual Switch Overview
ImageX Command-Line Options
Internet Protocol Version 6 Address Space
Link a Group Policy Object
Loopback processing with merge or replace
Manage Multiple, Remote Servers with Server Manager
Managing a Server Core installation: Overview
Migrate Print and Document Services to Windows Server 2012
Operations master roles
Print and Document Services Overview
Remote Desktop Gateway Manager
Sc config
Security Tools to Administer Windows Server 2012
Set up recovery actions to take place when a service fails
Storage Pools… Dive right in!
Switching Between the GUI and Server Core in Windows Server 2012
Troubleshooting Windows Firewall with Advanced Security in Windows Server 2012
Updating root hints
Using System Configuration (msconfig)
Using the Set-Service Cmdlet
Verifying Your Basic DNS Configuration
What’s New in Hyper-V for Windows Server 2012
What’s New in Hyper-V for Windows Server 2012
Windows and GPT FAQ