Friday, September 25, 2009

One or more features must be turned on before this feature cab be activated

Title: One or more features must be turned on before this feature cab be activated

Scenario: Trying to activate 'Office SharePoint Server Publishing' feature on the Site Features of the site.

Solution: Activate the 'Office SharePoint Server Publishing Infrastructure' feature at the site collection level first before trying the scenario.

Create Site Collection in a specific Content Database

Title: Create Site Collection in a specific Content Database

Scenario: My previous post on the same topic are some ways to create site collections in the specific content database. Programmatically we can also do it as mentioned in this blog post done by my friend Sandeep. So I kind of put them all in all and created a simple console application to create site collection in a specific content database.

Download the file and try for yourself.

Usage: use the command as show below.


Wednesday, September 23, 2009

Getting context using RunWithElevatedPrivileges

Title: Getting context using RunWithElevatedPrivileges


Scenario: Have you ever tried creating the SPSite and SPWeb context inside the RunWithElevatedPrivileges context? If you have done so, then you must have noticed that the context user is still the logged in user and is not elevated to the app_pool identity.
To get the right context, use the code as shown below.


Code:

SPSite siteColl = SPContext.Current.Site;
SPWeb webSite = siteColl.OpenWeb();

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteColl.ID))
{
using (SPWeb web = site.OpenWeb(site.ID))
{
// DO SOME THING IMP HERE
}
}
});

How to create Site Collection in a specific Content Database?

Title: How to create Site Collection in a specific Content Database?

Scenario:
We often have this requirement of creating sites collections in a specific content database instead of going into one of the available content databases. So, here are few available ways that can be considered. Every thing has its own disadvantages. So, pick the one which suits you the best.

Solution:
Option # 1 : While creating the new content database from CA you can set the options in Database Capacity Settings to limit the number of sites along with Warning. However it allows always 1 extra space for one more C-DB. So, you need proper planning well before for it. Ofcourse you can always go and increase it. So it might not be good working idea every time.





Option # 2 : As posted in my previous post use STSADM createsiteinnewdb option. Problem with this model is that you cannot create more than 1 site into the same CDB.


Option # 3 : Moving Site Collections between Content Databases


Per say if the site collection is created in an unwanted CDB, then you can move the site collection to the required CDB using the below statements. However, the issue with this model is you may not have the freedom to do it on production servers. So make sure you script it out properly and provide it to the Administrators.


STSADM.EXE -o enumsites -url http://mysiteiwantetomove > sites.xml



Open the sites.xml file and edit the file so that you would have only those site listing that you want to move.

STSADM.EXE -o mergecontentdbs -url http://mysiteiwantetomove -sourcedatabase
[databasename] -destinationdatabase [databasename] -operation 3 -filename sites.xml



Option # 4 : Take CDB's to offline mode


Go to CA > AM > CDB's and edit.
You can take all other CDB's to offline mode except the one you want to create site collections under. That way, your new site collection will be created in the CDB available in ready mode. Once you create the site collection, make sure you bring back the other CDB's to ready mode.

Saturday, September 19, 2009

Get all the sub sites under the current site

Scenario:
You want to get all the sub sites under a given site collection including sub sites under sub sites.
Scenario:
Use site.AllWebs to get the entire collection as shown below.
Code:

void GetAllSitesUnderSiteCollection(string siteUrl)
{
using (SPSite site = new SPSite(siteUrl))
{
foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine(web.Title + " ~ " + web.Url);
}
}
}

Create Site in New Database using stsadm (createsiteinnewdb)

Title: Create Site in new database using createsiteinnewdb
Code:

stsadm -o createsiteinnewdb -url http://yourownurl/ -owneremail owner@emailid.com -sitetemplate sts#1 -title "My Site Title" -databasename Create_Under_This_DB -ownerlogin NTLogin\Admin

Programmatically Delete Site Collection

Title: Programmatically Delete Site Collection
Code:

void DeleteSiteCollection(string requestUrl, string siteUrl)
{
SPSite site = new SPSite(requestUrl);
SPWeb web = null;
try
{
SPWebApplication webapp = site.WebApplication;
web = site.OpenWeb();
SPSiteCollection spSites = webapp.Sites;
webapp.Sites.Delete(siteUrl);
}
finally
{

}
}

Programmatically Create Site Collection

Title:Programmatically Create Site Collection
Code:

void CreateSiteCollection(string requestUrl, string siteUrl, string title, string description, uint lcid, string webTemplate, string ownerLogin, string ownerName, string ownerEmail)
{
SPSite site = new SPSite(requestUrl);
SPWeb web = null;
try
{
SPWebApplication webapp = site.WebApplication;
web = site.OpenWeb();
SPSiteCollection spSites = webapp.Sites;
webapp.Sites.Add(siteUrl, title, description, lcid, webTemplate, ownerLogin, ownerName, ownerEmail);
}
finally
{

}
}