Monday, April 12, 2010

Site Collections or Sub-Sites

Currently I’m working on Sri Lanka’s largest SharePoint implementation. According to the structure and the requirements of the project, my plan was to go for Site Collections. Specially the capacity and security. But I always like ideas from my team members which is helpful and knowledgeable in many different ways.
One of my team members Dinusha, came up with this article by Steve Goodyear.

Determining Between SharePoint Site Collections and Sub-Sites.

Sunday, April 11, 2010

SPoint.me

For people who have not heard of SPoint.me, it’s the premier social networking site for people in SharePoint industry. Yes, I know that info is not enough, right? smile_nerd

[Click the image below for a detailed description]
image

Visual Studio 2010 Launch

It’s less than 2 hours for me to step in to 12th of April, but according to the time zone difference there's few more hours for the big day, launch of Visual Studio 2010 at DevConnections.

image

Counting… fingerscrossed

Microsoft SharePoint 2007 Development – Sites and Webs


This article doesn’t cover what SharePoint is or its usage. Targeted audience is the programmers who are already familiar with SharePoint who’s looking for a programming introduction to sites and webs in SharePoint and learn how to interact with sites and site collections with the classes SPSite and SPWeb in SharePoint Object Model.
Simplifying Sites and Webs
At a glance, yes, it’s complicating. Though the Webs and Sites sound similar, in SharePoint these are two different concepts. The site, as far as SharePoint is concerned, is actually a site collection. Each site collection consists of one or more root websites. If you use the object model to create a site collection and you indicate that the template for that site collection should be a Team site, what you have actually done is created a site collection and a root website (SPWeb instance) that was provisioned from the Team Site template. So simply that means you cannot have a site collection that doesn’t have a root website. Two things you should never forget.
•SPSite class is the model for a collection of SPWeb objects.
•To get at the list of site collections on a given web application, you simply reference the Sites property of an instance of the SPWebApplication class.
Working with SPSite Class
SPSite class can be used to create, delete and updating of site collections. For the complete listing of all properties and methods, glance through the online SharePoint Software Development Kit (SDK) found on MSDN (http://msdn2.microsoft.com/en-us/library/aa905858.aspx).
Enough training in the shallow end, let’s get to the deep end. Hit VS 2008 and create a new windows application. Add reference to Microsoft.SharePoint.dll. Add few textboxes, labels and button to the windows form as given below.

image

Switch to the coding window.

image

Then write the below coding in the click event of the button. (TextBoxes have been used as in given in the order).
Let’s have a look at the coding. First I have created a SPSite object called site passing the absolute URL as a parameter. The text on txtSPSite textbox represents the absolute URL.


image 

Next step is to create a SPWebapplication object. Site creation is done by calling the Add() on the SPSiteCollection class instance. 
There are multiple overloads that allow you to supply progressively more information to create the new site collection. Okay, let’s see the parameters I have used in the Add() method. First it’s the siteUrl, next the Title of the site and the Site Description. LCID is the Local Identifier. Default is 1033 for U.S. English. Next comes the Template ID, followed by the owner login, owners name and finally the owners email address.
image

Given above is the windows form filled with sample data according to my SharePoint server.


image

If you can see the above message in your screen, Congratulations! You have successfully created a SharePoint site programmatically using SharePoint Object Model. Open your web browser http://sharepointsvr:4488/sites/TestNewSite/default.aspx [http://sharepointsvr:4488 is my test server] and you can see a screen similar the one given below. For your convenience I’ve provided the default web templates in the table below.

image

Template ID Description
STS#0 Team site
STS#1 Blank site
STS#2 Document workspace
MPS#0 Basic meeting workspace
MPS#1 Blank meeting workspace
MPS#2 Decision meeting workspace
MPS#3 Social meeting workspace
MPS#4 Multipage meeting workspace
WIKI#0 Wiki
BLOG#0 Blog

Updating a site is not easy as creating one. You need to get an instance of the SPWeb class for the root web of the site collection (which was created by default when you created a new site collection).

Working with SPWeb Class
SPWeb class provides most of the functionality of single websites, whereas the SPSite class is designed to deal with site collections.
Let’s add a subweb, a blog as a subweb to the parent site collection we created earlier. To add a new web to the root web of a site collection, you obtain a reference to that site and then call the Add() method on the AllWebs property.
image 

Type the code above in your windows application, make sure to add reference to the Microsoft.SharePoint.dll. Pay attention to the overloads of the Add() method. Hit F5 to test the code. If you see the message box with the message “Web created successfully”, yes you have successfully created a blog web.
image

You can obtain a reference to a SPWeb instance either by obtaining it through an indexer property on a SPWebCollection instance.

image

There are some properties on a website that cannot be changed after the site has been created. Changes made to a SPWeb instance are only saved after calling the Update() method. One last code block to change the title of the blog site we just created.


image
image

Summary
The SharePoint object model is so powerful that you have the full control over each and every component SharePoint provides. But honestly the hard task is to remember the methods and the properties of the classes.
All comments welcome. :)

Monday, April 5, 2010

Calculated Field Formulas in SharePoint

I’ve been looking for calculated field formulas for a SharePoint POC I was working on. With the help of a good friend Dinusha, I was lucky to get my hands on this valuable resource.

Calculated Field Formulas

Monday, March 29, 2010

SharePoint Designer Workflows does not start automatically in SharePoint

I’ve been playing with SharePoint Designer Workflows heavily  for the past few days. Suddenly I came across the problem of these workflows does not start automatically though I select the option to start the workflow when an item is created or modified in a list or library.
Browse the web and found the reason of installing Windows SharePoint Services 3.0 Service Pack 1 which caused the problem. This behavior occurs because a security fix in Windows SharePoint Services 3.0 SP1 prevents declarative workflows from starting automatically under the system account.

You could fall in to 1 of the 3 scenarios given below while you faced this problem…
1) The Windows SharePoint Services Web application runs under a user's domain account.
2) The user logs in by using this domain account.
3) The site displays the user name as System Account.
Answer / Solution
Set the application pool to use a different user account than System Account.
Step by Step Guide.
1) Open SharePoint Central Administration.
2) Go to Operations Tab.
3) Under Security Configurations click on Service Accounts.
4) Select “Web application pool” option button.
5) In the web service drop down box select “Windows SharePoint Services Web Application”.
6) And in the Application pool drop down, select the application pool you want.
7) Next select the “Configurable” option button and then type the user name and password of a user having rights to assign to an application pool.

image
8) Go to command prompt and type iisreset /noforce
9) Make sure to test the workflow log-in in to SharePoint other than System Account.

Happy SharePoint day with Workflow.. smile_nerd

Monday, March 15, 2010

Microsoft SharePoint 2007 Development – Features and Solutions

I thought of publishing all the Technical Articles I write for Magazines in my blog as in someway it’ll help the readers. Article below was written in the month of November for online magazine Digit. It’s the second article of the series Microsoft SharePoint 2007 Development.
You can find the 1st Article here.
Please click on the images for the larger preview.

Another cold, dark and cloudy day with thundering showers in the month of November. What else can come to my mind rather going to bed? Oh! the second article of the series, SharePoint 2007 Development. Hope you had a chance to go through the first article “SharePoint Object Model” which is an introduction to SharePoint Object Model.
This article doesn’t cover what SharePoint is or its usage. Targeted audience is the programmers who are already familiar with SharePoint who’s looking for an introduction to “Features” and learn how to access “Features” with SharePoint Object Model.

What is a Feature?

What exactly is a feature and why is it called a feature? Isn’t it bit awkward to talk about the “Feature” feature? :)
The Feature is a container of various defined extensions for SharePoint. It’s a set of XML files which are deployed to Web Front Ends (WFE) servers in a farm that can be bundled in site definitions or individually turned on in SharePoint sites. The ability for sites to use the functionality in other sites easily is provided by features. Features has eliminated copying and pasting of huge chunks of XML from one template to another. Features have a scope, and can be associated with various scopes in SharePoint: web, site, web application, or farm. Depending on the scope, different types of individual elements can be defined in XML.

What is a Solution?

Solutions allow to package features in a cabinet file (.cab) and store important metadata about the features. After a Solution is installed on a server in the farm, you can then use SharePoint’s Solution management features to automate the deployment of that Solution to other sites within the farm.
Great, now you have a basic understanding of what a feature is and what a solution is in SharePoint. As the next step I’m going to deploy a feature in to a MOSS 2007 (Microsoft Office SharePoint Server) server.

Creating a Simple Feature

This example steps you through the process of creating a custom action Feature that adds a button to the toolbar of the form for editing announcements items within the scope of a SharePoint site. Example below is taken from Windows SharePoint Services SDK.
Create a subfolder called SimpleFormToolbarButton folder under Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES in your MOSS Server.
Best way to do this without worrying about the permission issues is to create the folder in command prompt. Type cmd in Run, hit the Enter key. Browse to the Features folder and type md SimpleFormToolbarButton which will create a subfolder with the given name.
Next step is to create the feature.xml with in the newly created folder. So create a new file with the name feature.xml and type/paste the content below.
image 
Open the file within Visual Studio, but one thing you will notice is there is no IntelliSence. Click on Schemas in file properties. You will see a window to select a XML schema file. Browse to the XML folder under Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE, select the wss.xsd file to enable IntelliSence for the feature.xml.
To replace the GUID placeholder in the previous Id attribute, generate a GUID by Tools > Create GUID > Registry Format and click on copy button.
image 
Your code should look like above (with a different GUID). Next Create an Elements.xml file in the SimpleFormToolbarButton folder that points to an .aspx file, such as the following, which uses the CustomAction element to define the custom action.
image 
To test the sample feature, create a SampleUrl.aspx page such as the following that displays "Hello, world!" and place it in the TEMPLATE\LAYOUTS directory.
image 
Okay, let’s install the feature in the deployment server and activate it on a specified subsite. Go to the command prompt and type the below command for installation. Make sure to be within the Features folder.
stsadm -o installfeature -filename SimpleFormToolbarButton\Feature.xml
Activation can be done through the UI or through the command prompt. We’ll use the second method.
stsadm -o activatefeature -filename SimpleFormToolbarButton\Feature.xml -url http://Server/Site/Subsite
To try out the new button, navigate to the form for editing an existing announcements item. Great, if you followed me, you have successfully created, deployed and activated a simple feature. Image below show the customized toolbar with the link “Simple”.
image
Now is the time to play with the Object Model. SharePoint includes a robust object model for working with Features that allows developers to enumerate installed and activated Features, to turn Features on and off, and to control the installation or removal of Features.

Object Model for Features

Given below are the key classes.
§ SPFeatureCollection/SPFeature—Refers to a Feature state at a given site hierarchy level. The presence of a SPFeature instance within a property of type SPFeatureCollection indicates that the Feature is active at that level.
§ SPFeaturePropertyCollection/SPFeatureProperty—Represents a single property on a Feature or a collection of those properties.
§ SPFeatureScope—Represents an enumeration of the possible scopes in which Features can be activated. Possible values are: Farm, WebApplication, Site, and Web.
§ SPFeatureDefinition—Represents the basic definition of a Feature, including its name, scope, ID, and version. You can also store and retrieve properties of a Feature. Note that Feature properties apply globally to a single Feature definition, not to instances of Features activated throughout the farm.
§ SPFeatureDependency—Represents a Feature upon which another Feature depends.
§ SPElementDefinition—Represents a single element that will be provisioned when the Feature is activated.

Accessing Features

We are going to write a windows application to list the Features installed in a site. Create a new Windows Project and add reference to Microsoft.SharePoint.dll.
image
image 
Now let me explain few important lines in the code above. First you need to add 4 windows controls to the form.
Control Type Control Name
TextBox siteUrl
Button Button1
Label statusLabel
ListView faetureList
The dbConn contains the connection string to the SharePoint configuration database with credentials. _rootCollection will contains the SharePoint site url which will iterate through the features installed to check whether each feature is enabled or not in the GetFeatureEnabled method. Creating a new instance of the SPFarm class we are connecting to the Database with the given connection string. Then we iterate through the feature definitions which will be added in to the ListView as ListViewItems. Let’s see how it works, hit F5 to run the application.
image

Features and Feature Definitions

These two are not the same, and it is very important to know the difference. A Feature definition, as far as the object model is concerned, is an abstraction around the Feature manifest contained in a Feature directory in the Features directory. Feature definitions are installed at the farm (or server, if there is no farm) level.
A Feature is an instance of a Feature definition. And it’s features that can be activated or deactivated. Features have various levels of scope such as the site or web level.
To enumerate the list of Feature definitions that are currently installed within a farm, you need to use an instance of the SPFarm class and access the FeatureDefinitions property. To enumerate the list of active Features on a given site, you need to enumerate the Features property on the appropriate SPWeb or SPSite class instance.
As you can see what features are activated and which are not, you can do the activation and deactivation with the code. But it is the GUID that has to be provided.
image

SharePoint Object Model for Solutions

Other than the command line tool stsadm.exe you still can programmatically manipulate the list of solutions available with in a farm. All you need to do is open a connection to the farm by creating a SPFarm object and fire the Open method passing the database connation string as we did in the earlier example.
Adding a solution you have two options. You can pass the Solution filename as a parameter, or you can pass the Solution filename and a locale identifier (a UInt32, such as 1033):
image 
When removing a solution you can pass the solution name or the GUID for the remove method as a parameter. Iterating through the installed solutions in a farm can be done as given below.
image

Solution Deployment

Deploying solution with object model’s SPSolution class has two interesting methods which are the Deploy and the Retract methods. The Deploy method deploys a Solution to the given location, whereas the Retract method removes the Solution from the given location while still remaining installed within the farm.

Summary

This article included details on how to manipulate Features and Solutions programmatically using the SharePoint object model. If you followed me through the article, you should not only be able to create your own Features and Solutions, but you should also be able to install, manipulate, and deploy them programmatically.
You are welcome to put comments :)