Sunday, November 29, 2009

Import a SharePoint Designer Reusable Workflow

While trying to import the reusable workflow into Visual Studio, I got an error with message:


Parameter Name: relativePath

As a work around, I saved the entire site as template, saved the WSP file to my machine, and then imported only the required Workflow into Visual Studio. This workaround is described here:

http://msdn.microsoft.com/en-us/library/ee231580(VS.100).aspx

Any other alternatives to overcome the Parameter Name: relativePath error?

Chart Web Part

I used the new SharePoint 2010 "Chart Web Part" today, and I am already in love with it. As its name suggests, it is used to generate charts based on a data source (another web part, SharePoint List, BDC, Excel Services). It provides an easy to configure wizard that even a 10 year old kid can use. I will let the screen shots do most of the talking in this blog to give you a quick overview of its features and functionality.
To begin, you need to active go to Site Actions, Site Settings, Site collection features, and activate "SharePoint Server Enterprise Site Collection features".




On your page where you want to display the chart, click on Edit Page, Insert, Web Part. The web part is found under the Miscellaneous section.


It will add the Web Part to the page.


To begin configuration, click on "Data and Appearance".


Select "Connect Chart To Data".



Select "Connect to List" and click Next


Select your site and your List and click Next


If your list had, say Sales by Year data, you could use the "Filter Data" option to filter data to a particular year.


For this blog, I will not filter and instead use all the data present in the list.

Select your X and Y fields and click Finish.

To change the chart type, click on "Data and Appearance" and then "Customize Your Chart".



Click on "Standard Chart Types" tab.





There is a range of options. I will select "Pie" under Chart Type Categories.



There are different types of pie charts available. Select the one you wish to use and click on Next.

Select a suitable appearance matching your site theme, and the required size.




I plan to show a legend, so I decided to set the Width to be double of the Height.  I selected the following options:

Theme: Light Steel Blue
Chart Width: 800 px
Chart Height: 400 px
Chart Image Format: Jpeg

Note that the chart image type will decide the image type rendered on the page.

Check "Chart Title" and "Show Legend".



Set Chart Title as "Sales By Year" and set the position to Bottom Center.



Click "Finish" to exit the wizard and view the chart on the page.



Click on "Edit Web Part" to review and change the chart and other web part settings.



For basic charts and graphs scenarios, it is likely to replace Excel Services and SSRS. What do you think?

So simple, yet so powerful.

Friday, November 06, 2009

SharePoint Client Object Model

SharePoint Client Object Model is the new programming interface to access SharePoint data from a remote client machine.

Client OM unifies the programing model and can be called from JavaScript, .Net Code (Win Forms, Console App) or Silverlight Application.

Let’s program using the Client OM for a Win Forms application.

Add a reference to the following assemblies:

Microsoft.SharePoint.Client.Silverlight.dll
Microsoft.SharePoint.Client.Silverlight.Runtime.dll

Add a reference to the following namespace:
using Microsoft.SharePoint.Client;

Add 2 list boxes and a button onto your form.

To populate the List and the List Items, add the following code to the button click event:


string siteURL = "http://moss.contoso.com/sites/MySpace";

using (ClientContext ctx = new ClientContext(siteURL))
{
     //Get List Names
     Web site = ctx.Web;
     ctx.Load(site);
     ctx.Load(site.Lists);

     //Executes the current set of data retrieval queries and method invocations
     ctx.ExecuteQuery();

     foreach (List list in site.Lists)
     {
          listBox1.Items.Add(list.Title);
     }
}

//Get List Items from "Announcements" List
using (ClientContext ctx = new ClientContext(siteURL))
{
     string listTitle = "Announcements"; //listBox1.SelectedItem.ToString()
     Web site = ctx.Web;

     //Load List
     ctx.Load(site,
                s => s.Lists.Where(l => l.Title == listTitle));
     ctx.ExecuteQuery();
     List list = site.Lists[0];

     //Get items for the list

     CamlQuery query2 = new CamlQuery();
     ListItemCollection lc = list.GetItems(query2);
     ctx.Load(lc);
     ctx.ExecuteQuery();

     //Display Announcements Title
     foreach (ListItem item in lc)
     {
          listBox2.Items.Add(item["Title"].ToString());
     }
}

Note that you need to first Load(), and only then call ExecuteQuery().
Accessing the list item without calling Load and ExecuteQuery throws CollectionNotInitializedException.

Thursday, November 05, 2009

SharePoint 2010 Events

SharePoint 2010 introduces a number of different event receivers that developers can take advantage of.

There are 5 types of events which can be captured:
1. List Events
2. List Item Events
3. List Email Events
4. Web Events
5. List Workflow Events

After events can now be synchronous (Before events are synchronous and After events are asynchronous by default).

Visual Studio 2010 provides Event Receiver template. It will help developers quickly jumpstart the process.






The list of events is given below.

SPListEventReceiver: Provides methods to trap events that occur for lists.

ListAdded : Event that occurs after a list is added to a Web site.
ListAdding : This event is registered at the Web site or site collection where the list is created.
ListDeleting : Event that occurs before a list is deleted.
ListDeleted : Event that occurs after a list is added to a Web site.
FieldAdded : Occurs after a field link is added.
FieldAdding : Occurs when a field link is being added to a content type.
FieldDeleted : Occurs after a field has been removed from the list.
FieldDeleting : Occurs when a field is in the process of being removed from the list.
FieldUpdated : Occurs after a field link has been updated
FieldUpdating : Occurs when a field link is being updated


SPItemEventReceiver: Provides methods for trapping events that occur to items.

ItemAdded : Asynchronous After event that occurs after a new item has been added to its containing object.
ItemAdding : Synchronous Before event that occurs when a new item is added to its containing object.
ItemAttachmentAdded : Asynchronous After event that occurs after a user adds an attachment to an item.
ItemAttachmentAdding : Synchronous Before event that occurs when a user adds an attachment to an item.

ItemAttachmentDeleted : Asynchronous After event that occurs when after a user removes an attachment from an item.

ItemAttachmentDeleting : Synchronous Before event that occurs when a user removes an attachment from an item.

ItemCheckedIn : Asynchronous After event that occurs after an item is checked in.
ItemCheckedOut : Asynchronous After event that occurs after an item is checked out.
ItemCheckingIn : Synchronous Before event that occurs as a file is being checked in.
ItemCheckingOut : Synchronous Before event that occurs after an item is checked out.
ItemDeleted : Asynchronous After event that occurs after an existing item is completely deleted.
ItemDeleting : Synchronous Before event that occurs before an existing item is completely deleted.

ItemFileConverted

ItemFileMoved : Occurs after a file is moved.
ItemFileMoving : Occurs when a file is being moved.
ItemUncheckedOut : Synchronous Before event that occurs when an item is being unchecked out.
ItemUncheckingOut : Synchronous Before event that occurs when an item is being unchecked out.

ItemUpdated : Asynchronous After event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.

ItemUpdating : Synchronous Before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.


SPWebEventReceiver: Provides methods for trapping events that occur to Web sites.

SiteDeleted : Occurs after a site collection has been deleted.
SiteDeleting : Occurs when a site collection is being deleted.
WebDeleted : Asynchronous After event that occurs after an existing Web site is completely deleted.
WebDeleting : Synchronous Before event that occurs before an existing Web site is completely deleted.
WebMoved : Asynchronous After event that occurs after an existing Web site has been moved.

WebMoving : Synchronous Before event that occurs before an existing Web site has been renamed or moved to a different parent object.

WebAdding : Synchronous Before event that occurs before a new Web site is created.

WebProvisioned : Synchronous After event that fires after the Web is fully provisioned and the provisioning process has stopped.


SPEmailEventReceiver: Provides a method for trapping the event when a list receives an e-mail message.

EmailReceived : Occurs after an e-mail message has arrived.


SPWorkflowEventReceiver:

WorkflowCompleted : A workflow was completed
WorkflowPostponed : A workflow was postponed
WorkflowStarted : A workflow was started
WorkflowStarting : A workflow is starting
Note: This documentation is preliminary and is subject to change.

Tuesday, November 03, 2009

Visual Web Part is the new Smart Part

Visual Studio 2010 comes with a template for VisualWebPart.

It creates a web part (.cs), User Control (.ASCX) and other associated files.

The Web Part maintains a reference to the User Control and loads the same in the CreateChildControls method.

private const string _ascxPath = @"~/_CONTROLTEMPLATES/ContosoWebParts/FirstWebPart/MyUserControl.ascx";

protected override void CreateChildControls()
{
  Control control = this.Page.LoadControl(_ascxPath);
  Controls.Add(control);
  base.CreateChildControls();
}


The actual functionality can be developed in the Web User Control (MyUserControl.ascx) which can leverage the Visual Studio Designer


The approach is similar to Return of Smart Part for MOSS 2007, which we used to wrap ASCX file within an web part.

Sunday, November 01, 2009

STSADM SPMETAL

STSADM.EXE is now loacted at:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

From 12\BIN to 14\BIN, thats the only change, as there is no Version 13 (considered unlucky by many).

The new SPMETAL.EXE, which is used to genrate Entity Classes for LINQ to SharePoint, also resides inside 14\BIN.

Developer Dashboard for SharePoint Performance Monitoring

One of the new useful features SharePoint 2010 introduces is the Development Dashboard. It provides detailed diagnostic information for each page load at the bottom of the page. It is turned Off by default. The setting can be modified using SharePoint Object Model, STSADM.exe or PowerShell.


Below is the object model code snippet to change the settings using object model:

SPPerformanceMonitor SPPerfMon = SPFarm.Local.PerformanceMonitor;
SPPerfMon.DeveloperDashboardLevel = SPPerformanceMonitoringLevel.On;
//SPPerfMon.DeveloperDashboardLevel = SPPerformanceMonitoringLevel.OnDemand;
//SPPerfMon.DeveloperDashboardLevel = SPPerformanceMonitoringLevel.Off;
SPPerfMon.Update();

Setting it to On shows the page diagnostics, load time and other information at the bottom of the page.






Setting it to OnDemand makes the icon appear at the upper right hand side of the page. Clicking on the icon makes the Developer Dashboard appear and disappear on the page.

Using STSADM.exe, the scripts are:

stsadm -o setproperty -pn developer-dashboard -pv on
stsadm -o setproperty -pn developer-dashboard -pv off
stsadm -o setproperty -pn developer-dashboard -pv OnDemand


Everything that can be done in STSADM should be possible in PowerShell too. You can explore it on your own.
I am wondering if there is an option yet on the UI to manage the Developer Dashboard setting. Anyone knows?
The setting is only at the SPFarm level. It would be better if we we have an option to turn this on at the Site Collection or Site Level.


Updates:
The above code does not work on the updated SP2010 vesion. Try this instead:

SPWebService cs = SPWebService.ContentService;

cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand; //or On or Off
cs.DeveloperDashboardSettings.Update();

You need to refer to the Microsoft.SharePoint.Administration namespace.