Wednesday, May 12, 2010

SharePoint 2010 and Office 2010 Global Launch

Are you ready for the BIG DAY? SharePoint 2010 and Office 2010 Global Launch. Watch the keynote done by Stephen Elop, President of the Microsoft Business Division, join the virtual launch conversation, and participate in on-demand sessions where you’ll learn more about how Microsoft products can solve the unique productivity challenges you’re facing as you look to the future.


Keynote is focused on...
  • Deliver maximum productivity across PC, phone, and browser
  • Enhance IT choice and flexibility
  • Leverage a platform for developers to build innovative solutions

Tuesday, May 11, 2010

Microsoft SharePoint 2007 Development – Fun with Lists

List Management

List is more like a Table in a Database. Of all the tasks a SharePoint programmer can do in SharePoint, working with Lists is the mostly common task.
Namespace: Microsoft.SharePoint
Classes: SPList, SPListItem, SPListItemCollection

With MOSS 2007 and WSS, we get different kinds of lists. To name them…
. Announcements list
. Contacts list
. Discussion Board list
. Links list
. Calendar list
. Tasks list
. Project Tasks list
. Issue Tracking list
. Survey list
. Custom list
. Custom list in Datasheet view
. KPI list (SharePoint Server 2007 only)
. Languages and translators (SharePoint Server 2007 only)
. Import spreadsheet
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 Lists in SharePoint and leverage the features of SPList and SPListCollection classes in SharePoint Object Model.

Enumerating Lists
Yes you see it right, its Lists. Before getting to the list items, let’s try to get a collection of lists implemented in a site. So basically in other words we are trying to enumerate through any SPListCollection instance.
First we need to create a SPSite object and get all webs in to a SPWeb object by calling the AllWebs() method of the SPSite object. Then it’s just a matter of iterating through the SPLists objects available in the SPWeb object.
Create a new C# console application and paste the code below changing the SPSite class’s input parameter url.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace AllLists
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://dc:8844");
SPWeb web = site.AllWebs[0];
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "*" : "",
list.Title, list.ItemCount);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine("{0}", list.Description);
Console.WriteLine("................................");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
Console.ReadKey();
}
}
}
image 

Enumerating list items in Lists

It is the SPList you have to iterate through to find the SPListItem. But you might run in to some errors if you do the list iteration against a top level site collection instead of a team site.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace AllLists
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://dc:8844/sales/");
SPWeb web = site.AllWebs[0];
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "*" : "",
list.Title, list.ItemCount);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine("{0}", list.Description);
Console.WriteLine("------------------------");
foreach (SPListItem item in list.Items)
{
Console.WriteLine("\t * {0}", item.Title);
}
Console.WriteLine("\n");
Console.WriteLine("................................");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
Console.ReadKey();
}
}
}

Make sure you add reference to Microsoft.SharePoint.dll assembly prior writing the code above.

Managing Lists

When working with Lists, the source container is always an object created from SPListCollection. Given code give below, I create two Task Lists, selecting the “Task” template.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace ManagingLists
{
class Program
{
static void Main(string[] args)
{
SPSite rootSite = new SPSite("http://dc:8844");
SPWeb rootWeb = rootSite.AllWebs[0];

SPListTemplate sourceTemplate = rootWeb.ListTemplates["Tasks"];
Guid newListGuid = rootWeb.Lists.Add("My First Task List",
"My First Task List", sourceTemplate);
Guid secondNewListGuid = rootWeb.Lists.Add("My Second Task List",
"My Second Task List", sourceTemplate);
SPList newList = rootWeb.Lists[newListGuid];
SPList secondNewList = rootWeb.Lists[secondNewListGuid];
secondNewList.Delete();
newList.Description = "My Custom Task List - From Code";
newList.Update();
Console.WriteLine("List creation completed.");
Console.ReadLine();
}
}
}

Browse your SharePoint site, where you could see both the Task Lists you created using code above.

Managing List Items

Managing List Items is same as managing Lists. Okay, let’s add a task item in to task list in code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ManagingListItems
{
class Program
{
static void Main(string[] args)
{
SPSite rootSite = new SPSite("http://dc:8844");
SPWeb web = rootSite.AllWebs[0];
SPList taskList = web.Lists["My Task List"];
SPListItem newTask = taskList.Items.Add();
newTask["Title"] = "My Task List Title";
newTask["DueDate"] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(30));
newTask["PercentComplete"] = 0.1;
newTask.Update();

newTask = taskList.Items.Add();
newTask["Title"] = "This Task will be deleted.";
newTask.Delete();
taskList.Update();

Console.WriteLine("Work completed.");
Console.ReadLine();
}
}
}

Summary
The SharePoint object model is so powerful that you have the full control over each and every component SharePoint provides. This article provides you an entry point to accessing and managing SharePoint Lists in code.

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