Monthly Archive April 2011

AvatarByRashid Aziz

Create sub site programmatically in SharePoint 2010

Sometime we require creating a sub site programmatically in code. Here is the simple way you can create a sub site programmatically.

(SPSite site = new SPSite(http://siteCollectionUrl))

  { using (SPWeb web = site.OpenWeb())

        {try
            { web.AllowUnsafeUpdates = true;
               //Create new web site
               SPWeb newWeb = site.AllWebs.Add("Site url", " Site name", "Site description", 1033, {Name of the Site Template}, true, false); 
               newWeb.Update();
            }
            catch (Exception)  { }
            finally { web.AllowUnsafeUpdates = false; } 
             } }

 

Name of the site templates

E.g. Use “STS#0” for Team site or “BLOG#0” for Blog site.

Available site templates

Site Template Names

 

Team Site

 

STS#0

Blank Site

STS#1

Document Workspace

STS#2

Blog

BLOG#0

Group Work Site

SGS#0

Visio Process Repository

VISPRUS#0

Basic Meeting Workspace

MPS#0

Blank Meeting Workspace

MPS#1

Decision Meeting Workspace

MPS#2

Social Meeting Workspace

MPS#3

Multipage Meeting Workspace

MPS#4

Assets Web Database

ACCSRV#1

Charitable Contributions Web Database

ACCSRV#3

Contacts Web Database

ACCSRV#41

Issues Web Database

ACCSRV#6

Projects Web Database

ACCSRV#5

Document Center

BDR#0

Records Center

OFFILE#1

Business Intelligence Center

BICenterSite#0

My Site Host

SPSMSITEHOST#0

Personalization Site

SPSMSITE#0

Enterprise Search Center

SRCHCEN#0

Basic Search Center

SRCHCENTERLITE#0

FAST Search Center

SRCHCENTERFAST#0

Enterprise Wiki

ENTERWIKI#0

Publishing Portal

BLANKINTERNETCONTAINER#0

Publishing Site

CMSPUBLISHING#0

Publishing Site With Workflow

BLANKINTERNET#2

AvatarByRashid Aziz

Modify web.config settings on feature activation in SharePoint

In SharePoint 2010 there is a way to modify web.config settings file. You need to use SPWebConfigModification class which is a part of Microsoft.SharePoint.Administration namespace. This class allows us to register entities dynamically.

Adding new settings in web.config

I am going to use SPWebModification class inside “Feature Activating” method of SharePoint feature to register a custom assembly.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebService newService = SPWebService.ContentService;

SPWebConfigModification customModification = new SPWebConfigModification();
customModification.Path = "configuration/SharePoint/SafeControls";
customModification.Name = "SafeControl[@Assembly='CustomAssembly'][@Namespace='CustomNamespace'][@TypeName='*'][@Safe='True']";
customModification.Sequence = 0;
customModification.Owner = "UserName";
customModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
customModification.Value = "<SafeControl Assembly='CustomAssembly' Namespace='CustomNamespace' TypeName='*' Safe='True' />";
newService.WebConfigModifications.Add(customModification);
 
newService.Update();
newService.ApplyWebConfigModifications();

}

Now you need to deploy and activate the feature. After that open web.config of your SharePoint web application where you deployed the feature and will find the following setting being added.J

<SafeControl Assembly="CustomAssembly" Namespace="CustomNamespace" TypeName="*" Safe="True" />
AvatarByRashid Aziz

Embed YouTube video in SharePoint 2010

Some time you need to add YouTube videos in your SharePoint site pages, it is very simple to implement. I’m going to show step by step how to embed YouTube videos in SharePoint 2010 site, using content editor web part.

1.   Go to YouTube video which you want to embed and copy the video embedded code , should be something like this,

<iframe title=”YouTube video player” width=”480″ height=”390″ src=”http://www.youtube.com/embed/CuLxW5sPXhA?rel=0″ frameborder=”0″ allowfullscreen></iframe>

2.   Open notepad and past the above code there, and save the file with name something like “videotest.txt”.

3.   Go to SharePoint site and open a document library and upload the “videotest.txt” file.

4.   Now go to the page where you want to embed the video and add a content editor web part on page.

5.   Go to the document library where you already added “videotest.txt” file, right click on file and click on “copy shortcut”.

6.   Now go back to the page where you have added the content editor web part, click on web part and select “Edit webpart”.

7.   In the “custom link” property paste the “videotest.txt” shortcut value(already copied in step 5)

8.   Click Ok.

 

You can see in the below picture that the YouTube video is embedded in SharePoint page.