Saturday, July 21, 2012

Feature activation and deactivation

Before learning about Feature Activation and Deactivation, we need to first learn about Features.



There are few key terms that you need to remember when it comes to working with Features deployment.
1. Install
2. Activate
3. Uninstall


4. Deactivate
When working with Visual Studio, deployment a feature is as easy is right clicking your project and selecting Deploy from context menu (assuming all the default settings are in place and there are no build errors)

However you will not have the luxury of Visual Studio when it comes to either staging or production environments. You will have to do something new to deploy your features. Learn PowerShell commends.

Once you have your feature developed, tested and ready to deploy, click Start> All Programs> Microsoft SharePoint 2010 Products> SharePoint 2010 Management Shell

Install a feature
Install-SPFeature -path "MyCustomFeature"
Activate a feature
Enable-SPFeature –identity "MyCustom" -URL http://somesite
Deactivate a feature
Disable-SPFeature –identity "MyCustom" -URL http://somesite
Uninstall a feature
Uninstall-SPFeature -path "MyCustomFeature"

My personal favorite link which explains Solution deployments instead of Features is a very useful link.

 


Features and Solutions in SharePoint 2010

SharePoint 2010 (SharePoint Foundation 2010) which is the later version of WSS 3.0 has something called Features and Solutions. The concept is of course not now to SharePoint but I assume that it is new for the readers.
Features:
A SharePoint feature is a collection of SharePoint elements that can help a user accomplish a particular goal or task. Features can also be described as logical groups of elements that together serve a common purpose (Wrox)

Features are units of functionality which have scope, can be installed, activated, uninstalled and or deactivated from your scopes.

Scopes of a feature are
1. Farm
2. Web Application
3. Site
4. Web

A feature can be a list definition, list instance, event handler, custom navigation, a list or document library, custom master page, page layouts, application pages, custom webpart or workflows.

So a feature is a logical grouping which says when I deploy this solution make sure that the items in a feature can be deployed and activated as a single unit because they depend on each other; as I said earlier, its a unit of functionality.


To work with feature activation and deactivation, refer my next post.

Solutions:
A collection of features to be deployed in your SharePoint environment. Also known as WSP (Windows SharePoint Solution Package) which is kind of a installer for SharePoint.

You must be aware of two types of solutions in SharePoint 2010 - Sandboxed and Farm. When you first create your SharePoint projects in Visual Studio your package or your solutions (.wsp) file is dependent of the choice you made earlier while creating your project in Visual Studio.Once you have selected a project deployment type (i.e. farm or sandbox) do not worry, Visual Studio lets you change your decision if needs be.

Similar to features, Solutions too can be installed, activated, deactivated and uninstalled using Visual Studio and PowerShell  (which is introduced in SharePoint 2010 as oppose to MOSS 2007 stsadm)

To work with Solution activation and deactivation, refer to my next post.

While researching about Features and Solutions, I came across a very good post and recommend beginners to read and implement it.

Friday, July 20, 2012

SPWeb in SharePoint 2010

Often confused with a site collection within a web application, SPWeb represents an actual web site within a site collection (SPSite) which holds actual contents like pages, document libraries, lists .

At the top we have a web application, or a SPWebApplication
After SPWebApplication, we have site collection or site or SPSite
In each site collection we have web sites or SPWeb
In each of these sites we can have more sub sites also called SPWeb.


Lets see what our object model representation is in terms of Central Administration
1. When you click New under Central Administration>Application Management> Manage web applications, you are dealing with SPWebApplication. While creating a web application, you select Authentication type, port, host header and other settings.

2. When you click Create site collections under Central Administration> Application Management, you are dealing with SPSite (you select a web application to create site collection in it). While creating a site collection, you select an existing web application and choose site templates.

3. When you click New Site under Site Actions from your site, you are dealing with SPWeb. While creating a web site, you select a from available templates, title and url

SPSite object can be obtained in many ways. For example

SPSite siteCollection = new SPSite("SITE COLLECTION URL");
SPWeb web = SPContext.Current.Web;

SPSite siteCollection2 = new  SPSite("SITE COLLECTION URL");
SPWeb web2 = siteCollection2.OpenWeb("WEB URL");

SPSite siteCollection3 = new  SPSite("SITE COLLECTION URL");
SPWEB web3 = siteCollection3.RootWeb
******************************************************
Sample Code
using (SPSite ositeCollection = new SPSite(SITE URL))
            {
                using (SPWeb oweb = ositeCollection.RootWeb)
                //using (SPWeb oweb = ositeCollection.OpenWeb("PressReleases"))
                {
                    Console.WriteLine("Is Root Web: " + oweb.IsRootWeb);
                    Console.WriteLine("Master URL: " + oweb.MasterUrl);
                    Console.WriteLine("Name: " + oweb.Name);
                    Console.WriteLine("Site Port: " + oweb.Site.Port);
                    Console.WriteLine("Theme: " + oweb.Theme);
                    Console.WriteLine("Title:" + oweb.Title);
                    oweb.Title = "New Title";
                    oweb.Update();
                    Console.WriteLine("Title:" + oweb.Title);
                    Console.WriteLine("User is Web Admin: " + oweb.UserIsWebAdmin.ToString());
                    Console.WriteLine("Web Template: " + oweb.WebTemplate);
                    foreach (SPUser user in oweb.AllUsers)
                    {
                        Console.WriteLine("User Login Name: " + user.LoginName);
                    }

                    string templateName = "Test Site";
                    string templateTitle = "Test Template";
                    string templateDesc = "This template was saved programmatically.";
                    Console.WriteLine("Save as template");
                    oweb.SaveAsTemplate(templateName, templateTitle, templateDesc, false);
                    Console.WriteLine("Saved");
                }
                //Console.WriteLine("Start Creating site from code. Should run only once");
                //ositeCollection.RootWeb.Webs.Add("FromCode");
                //ositeCollection.RootWeb.Update();
                //Console.WriteLine("Site Created");
                //using (SPWeb oweb = ositeCollection.OpenWeb("FromCode"))
                //{
                //    Console.WriteLine("Is Root Web: " + oweb.IsRootWeb);
                //    Console.WriteLine("Master URL: " + oweb.MasterUrl);
                //    Console.WriteLine("Name: " + oweb.Name);
                //    Console.WriteLine("Site Port: " + oweb.Site.Port);
                //    Console.WriteLine("Theme: " + oweb.Theme);
                //    Console.WriteLine("Title:" + oweb.Title);
                //    Console.WriteLine("User is Web Admin: " + oweb.UserIsWebAdmin.ToString());
                //    Console.WriteLine("Web Template: " + oweb.WebTemplate);
                //    Console.WriteLine("Lists Collection");
                //}
            }

It is also available in SandBoxed solutions.

Related Links
SPContext
SPSite

SPSite in SharePoint 2010

Often confused with a site within a site collection, SPSite represents a collection (including top level web site annd all its sub sites. not a site.

At the top we have a web application, or a SPWebApplication
After SPWebApplication, we have site collection or site or SPSite
In each site collection we have web sites or SPWeb
In each of these sites we can have more sub sites also called SPWeb.


Lets see what our object model representation is in terms of Central Administration
1. When you click New under Central Administration>Application Management> Manage web applications, you are dealing with SPWebApplication. While creating a web application, you select Authentication type, port, host header and other settings.
2. When you click Create site collections under Central Administration> Application Management, you are dealing with SPSite (you select a web application to create site collection in it). While creating a site collection, you select an existing web application and choose site templates.
3. When you click New Site under Site Actions from your site, you are dealing with SPWeb. While creating s web site, you select a from available templates, title and url


SPSite object can be obtained in many ways. For example

SPSite siteCollection = new SPSite("SITE COLLECTION URL");
SPSite siteCollection = SPContext.Current.Site;
******************************************************
Sample Code
using (SPSite oSiteCollection = new SPSite(SITE URL))
            {
                Console.WriteLine("All Webs Count: " + oSiteCollection.AllWebs.Count.ToString());
                Console.WriteLine("Content Database Name: " + oSiteCollection.ContentDatabase.Name);
                Console.WriteLine("Features Counts: " + oSiteCollection.Features.Count.ToString());
                Console.WriteLine("Host Name: " + oSiteCollection.HostName);
                Console.WriteLine("Owner Login Name: " + oSiteCollection.Owner.LoginName);
                Console.WriteLine("Root Web Exists: " + oSiteCollection.RootWeb.Exists.ToString());
                Console.WriteLine("URL: " + oSiteCollection.Url);
                Console.WriteLine("Web Application Application Pool Name: " + oSiteCollection.WebApplication.ApplicationPool.Name);
            }

It is also available in SandBoxed solutions.

Related Links
SPContext
SPWeb

SPContext in SharePoint 210

Before discussing what SPContext is, we need to understand what a Context is.
As stated by free dictionary, context means :The circumstances in which an event occurs.
So if we take things in context and try to dig deeper in SPContext in SharePoint 2010 or any other version, it represents a context of your HTTP Request in Microsoft SharePoint Foundation.

SPContext object is used in your custom code like custom web parts and it returns context information in which your code is running.

For example you need to read some items from a list. There are 2 ways to do it. Either hard code the url to get to your Web Application/Site Collection/List or use SPContext to get the required object and use it accordingly.

Once you deploy your custom webpart in your web application and try to use it on a page, this site/site collection/web application becomes your context because your code in running inside this context.

The most important members of SPContext is Current
Once you get current context, you can use further members of the class

SPWeb web = SPContext.Current.Web;
SPSite site = SPContext.Current.Site;
SPList list = SPContext.Current.List;
SPListItem listItem = SPContext.Current.Item;

One important thing is that it is also available in SandBoxed solutions

Related Links
SPSite
SPWeb