Friday, January 18, 2013

Create Sites Using Custom Site Templates in SharePoint 2010


Introduction

In this article we will see how to create sites using custom site templates programmatically in the SharePoint 2010 Server Object Model.

Steps

The following code shows how we usually create a site using the default SharePoint 2010 templates/definitions.

Creating-site-in-SharePoint1.jpg

The "SPWeb" object's "Add" method creates a Web Site object with the specified site-relative URL, title, description, locale ID  and site definition or site template name. "STS#1" represents the blank site template.

We will create a sample UI page to get the inputs as shown below.

Creating-site-in-SharePoint2.jpg

Once the site has been created successfully, we can navigate to the site and check whether the blank site was created or not.

Creating-site-in-SharePoint3.jpg

But we need to create a site using a custom template. For demo purpose we use any existing site and store it as "Site Template" in the "Solution Gallery" and activate the solution. We will see the solutions in the following navigation path for any root site. All the solution files are saved in .wsp file format.

Path: "Site Actions" --> "Site Settings" --> "Solutions"

Next we need to get the solution/template id dynamically from the solution gallery. We will have the following code to get the template id and write it as a helper function.

Creating-site-in-SharePoint4.jpg

Usually many templates are available in the collection. If required we will pass the "Solution Name" as input and get the particular template id.

Creating-site-in-SharePoint5.jpg

Finally we need to pass the result template Id to create a new site, instead of default templates. It'll create a new site using our custom template.

Creating-site-in-SharePoint6.jpg

Summary

Suppose we have a large site with hundreds of lists and a document library, it's really easy to get the entire site structure as a template and using this method we can use the same custom template to create new sites programmatically.

Notes
  • Before we try this logic, we need to ensure that the "SharePoint Server Publishing Infrastructure" feature is activated at the site collection and site level.
  • The Site template will not have any information about the sub-sites and its content.
  • Before we save the site as "Site Template", we need to check to determine if any custom features are deployed and activated. Because while we use the same template , it'll expect the same features need to be activated.
  • The entire sample code is attached here.

Friday, January 4, 2013

Chrome control in apps for SharePoint 2013



Introduction:

        In this post we will see the basics about “Chrome Control” in apps for SharePoint 2013.


About Chrome Control:

                The Chrome Control downloads a copy of the style sheet used in the parent site and applies it to the HTML pages in your client-side App. We can find the default style for any site in the below URL format.

http://{SharePointSite}/_layouts/15/defaultcss.ashx

Chrome control basically apply style sheet to App page.

Steps:
                We need to dynamically load the "SP.UI.Controls.js" file, which is available in “/15 hive” path. We load this script using Jquery library as mentioned below.




Then create a collection to hold the chrome control option values. 


Finally, instantiate a new Navigation object using the container <div> added to the page markup in the previous step and make the object visible.


We can combine all these scripts into a single file called “ChromeLoader.JS” and refer this JS in our HTML/ASPX page and mention a div with “chrome_ctrl_container” id.

Now if we run the page we will get the below output.

The Navigation Bar with setup icon and menu will be loaded as per our configuration mentioned above.


The same settings will be configured directly in HTML as in below image and the output will be the same.

 

Summary:

            Chrome control in SharePoint 2013 App allows addition of custom navigation nodes that links to other pages and help pages. The demo App in Office 365 hosted App.