Tuesday, March 29, 2011

SPWeb Vs SPSite Vs SPWebApplication

If you are a beginner to SharePoint its vital to get an clear knowledge and should have a clear idea to distinguish SPWeb and SPSite and SPWebapplication. If you have a SharePoint environment setup in your machine try writing the following simple console application. This will go beyond the definitions and will be ease to understand and clarify these three buzz words.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace MyFirstTest
{
class Program
{
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplicationCollection webappcol = SPWebService.ContentService.WebApplications;
foreach (SPWebApplication webapp in webappcol)
{
Console.WriteLine("Web Application URL :" + webapp.Name);

foreach (SPSite site in webapp.Sites)
{
Console.WriteLine("Site Collection URL :" + site.Url);
Console.ReadKey();

foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine("Site URL :" + web.Url);
Console.ReadKey();
}
}
}

});
}
}
}

OutPut:

Web Application URL : Sharepoint-80

Site Collection URL : http://cd-Home

Site URL : http://cd-Home
http://cd-Home/Test
http://cd-Home/Test/Inside

Note:Since I am getting the "site.AllWebs" property it will iterate through all the webs in the Site Collection

Try in your environment you can understand the difference.....

Monday, March 7, 2011

Error occurred in deployment step 'Activate Features': Invalid field name.

Error occurred in deployment step 'Activate Features': Invalid field name. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

The above error may be a common problem on the deployment step, where the project contains site columns and content type/lists. The content type or list will be referencing the site column.

Cause :
There are three reasons for the above behavior.
1. If you have already deployed the solution and you get the error on later attempt, then the declarative mark-up of Field definition/List Schema could be modified from the version you already deployed to the site. Because the content db is queried for a non-existent object, where at that point it throws an error.

2. Referencing site column(field) from content type/lists cannot be resolved by the  deploying thread. Your feature will be structured as below. The referee column located in the “Elements” Module is referred from “Test ContentType” Module. On deployment the feature is activated in the following order.

3. The template/structure of the schema/field file is wrong.

Solution :
1.For the former case, make sure that you retract the package with the same version of above files and then modify the changes in fields/schema to get the newer version effect.

2. For the latter case, the site column(field) should be already deployed in the site before it’s being used by any other module(Test ContentType). In order to do that you don’t need to go for another feature, but simply you can change the order in the feature. In this case, the deployable feature takes the following form.

3. For the third case, rectify the issue in the schema/fields file.