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(“”);

                                }

                            }

                        }

                    }

 

Good to Know: Windows Server 2012 – Part 4

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

Add-DnsServerDirectoryPartition http://technet.microsoft.com/en-us/library/jj649942(v=wps.620).aspx
Add-DnsServerPrimaryZone http://technet.microsoft.com/en-us/library/jj649876(v=wps.620).aspx
Adding a Reverse Lookup Zone http://technet.microsoft.com/en-us/library/cc961414.aspx
Add-PswaAuthorizationRule http://technet.microsoft.com/en-us/library/jj592890(v=wps.620).aspx
Add-VMRemoteFx3dVideoAdapter http://technet.microsoft.com/en-us/library/hh848520(v=wps.620).aspx
Authorize a DHCP server in Active Directory http://technet.microsoft.com/en-us/library/cc759688(v=ws.10).aspx
Block Inheritance http://technet.microsoft.com/en-us/library/cc731076.aspx
Configure a Server Core Server with Sconfig.cmd http://technet.microsoft.com/en-us/library/jj647766.aspx
Configure Memory and Processors http://technet.microsoft.com/en-us/library/cc742470.aspx
Delegate Control of an Organizational Unit http://technet.microsoft.com/en-us/library/cc732524.aspx
Deploy Clustered Storage Spaces http://technet.microsoft.com/en-us/library/jj822937.aspx 
Deploying a GlobalNames Zone http://technet.microsoft.com/en-us/library/cc731744.aspx
Deploying Microsoft RemoteFX on a Single Remote Desktop Virtualization Host Server Step-by-Step Guide http://technet.microsoft.com/en-us/library/ff817586(v=ws.10).aspx
Deploying Network Load Balancing (NLB) and Virtual Machines on Windows Server 2008 R2 http://blogs.msdn.com/b/clustering/archive/2010/07/01/10033544.aspx
Deployment Image Servicing and Management Command-Line Options http://technet.microsoft.com/en-us/library/dd744382(v=ws.10).aspx
Dsadd http://technet.microsoft.com/en-us/library/cc753708(v=ws.10).aspx
Enable-VMRemoteFXPhysicalVideoAdapter http://technet.microsoft.com/en-us/library/hh848506(v=wps.620).aspx
Enforce a Group Policy Object Link http://technet.microsoft.com/en-us/library/cc753909.aspx
Evaluation Versions and Upgrade Options for Windows Server 2012 http://technet.microsoft.com/en-us/library/jj574204.aspx
Evaluation Versions and Upgrade Options for Windows Server 2012 http://technet.microsoft.com/en-us/library/jj574204.aspx
Get-DnsServerDiagnostics http://technet.microsoft.com/en-us/library/jj649883(v=wps.620).aspx
Grant a Member the Right to Logon Locally http://technet.microsoft.com/en-us/library/ee957044(v=ws.10).aspx
How to remove data in Active Directory after an unsuccessful domain controller demotion http://support.microsoft.com/kb/216498
How User Account Control Works http://technet.microsoft.com/en-us/library/jj574202.aspx
Hyper-V Dynamic Memory Configuration Guide http://technet.microsoft.com/en-us/library/ff817651(v=ws.10).aspx
Hyper-V Virtual Switch Explained, Part 2 http://www.altaro.com/hyper-v/hyper-v-virtual-switch-explained-part-2/ 
Hyper-V Virtual Switch Overview http://technet.microsoft.com/en-us/library/hh831823.aspx
ImageX Command-Line Options http://technet.microsoft.com/en-us/library/cc749447(v=ws.10).aspx
Install-PswaWebApplication http://technet.microsoft.com/en-us/library/jj592894(v=wps.620).aspx
Install-RemoteAccess http://technet.microsoft.com/en-us/library/hh918408(v=wps.620).aspx
Internet Protocol Version 6 Address Space http://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xml
Link a Group Policy Object http://technet.microsoft.com/en-us/library/cc732979.aspx
Loopback processing with merge or replace http://technet.microsoft.com/en-us/library/cc782810(v=ws.10).aspx
Manage Multiple, Remote Servers with Server Manager http://technet.microsoft.com/en-us/library/hh831456.aspx
Managing a Server Core installation: Overview http://technet.microsoft.com/en-us/library/ee441255(v=ws.10).aspx
Migrate Print and Document Services to Windows Server 2012 http://technet.microsoft.com/en-us/library/jj134150.aspx
Operations master roles http://technet.microsoft.com/en-us/library/cc773108(v=ws.10).aspx
Print and Document Services Overview http://technet.microsoft.com/en-us/library/hh831468.aspx
Remote Desktop Gateway Manager http://technet.microsoft.com/en-us/library/cc725706.aspx
Remove-ADComputer http://technet.microsoft.com/en-us/library/ee617250.aspx
Remove-WindowsFeature http://technet.microsoft.com/en-us/library/ee662310.aspx
Sc config http://technet.microsoft.com/en-us/library/cc990290(v=ws.10).aspx
Security Tools to Administer Windows Server 2012 http://technet.microsoft.com/en-us/library/jj730960.aspx
Set up recovery actions to take place when a service fails http://technet.microsoft.com/en-us/library/cc738230(v=ws.10).aspx
Set-BCAuthentication http://technet.microsoft.com/en-us/library/hh848404(v=wps.620).aspx
Set-DnsServer http://technet.microsoft.com/en-us/library/jj649845(v=wps.620).aspx
Set-DnsServerDsSetting http://technet.microsoft.com/en-us/library/jj649874.aspx
Set-DnsServerForwarder http://technet.microsoft.com/en-us/library/jj649887(v=wps.620).aspx
Set-DnsServerSetting http://technet.microsoft.com/en-us/library/jj649909.aspx
Set-GPPermissions http://technet.microsoft.com/en-us/library/ee461038.aspx
Set-WSManInstance http://technet.microsoft.com/en-us/library/hh849875.aspx
Set-WSManQuickConfig http://technet.microsoft.com/en-us/library/hh849867.aspx
Storage Pools… Dive right in! http://blogs.technet.com/b/canitpro/archive/2012/12/13/storage-pools-dive-right-in.aspx
Switching Between the GUI and Server Core in Windows Server 2012 http://www.petri.co.il/switching-gui-server-core-windows-server-2012.htm
Troubleshooting Windows Firewall with Advanced Security in Windows Server 2012 http://social.technet.microsoft.com/wiki/contents/articles/13894.troubleshooting-windows-firewall-with-advanced-security-in-windows-server-2012.aspx
Updating root hints http://technet.microsoft.com/en-us/library/cc758353(v=ws.10).aspx
Using System Configuration (msconfig) http://windows.microsoft.com/en-us/windows-vista/using-system-configuration
Using the Set-Service Cmdlet http://technet.microsoft.com/en-us/library/ee176963.aspx
Verifying Your Basic DNS Configuration http://technet.microsoft.com/en-us/library/cc959303.aspx
What’s New in Hyper-V for Windows Server 2012 http://technet.microsoft.com/en-us/library/hh831410.aspx
What’s New in Hyper-V for Windows Server 2012 http://technet.microsoft.com/en-us/library/hh831410.aspx
Windows and GPT FAQ http://msdn.microsoft.com/en-us/library/windows/hardware/gg463525.aspx