Archive

Posts Tagged ‘sharepoint2010’

Automatically connect WebParts on a SharePoint page

August 6th, 2010 Jeffrey Tummers 3 comments

For a recent project we had several WebParts (Provider and Consumer webparts) on a page which had to be connected to each other when the page was provisioned by our solution.

Unfortunately this cannot be done within the Module.xml :(

So I created a method which automatically connects the consumer WebParts to their correct provider WebParts.

To do this I basically used the SPLimitedWebPartManager, ProviderConnectionPoint, ConsumerConnectionPoint and some other nice classes.

The method (warning long block of code)

/// <summary>
/// Connects the web parts on page.
/// </summary>
/// <param name="web">The web.</param>
/// <param name="pageUrl">The page URL.</param>
/// <param name="publish">if set to <c>true</c> [publish].</param>
public static void ConnectWebPartsOnPage(SPWeb web, string pageUrl, bool publish)
{
	// get the page from the url
	SPFile page = web.GetFile(pageUrl);

	// make sure the page exists
	if (page.Exists)
	{
		// if the page is checked out, check it in because then the user can do an rollback
		if (page.Level == SPFileLevel.Checkout)
		{
			page.CheckIn("Page was checked out, needs to be checked in before connecting webparts", SPCheckinType.MinorCheckIn);
		}

		// check out the page because we are going to do changes on it
		page.CheckOut();

		// get the webpartmanager for the page
		using (Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
		{
			try
			{
				// try to connect the webparts on the page
				if (ConnectWebParts(mgr))
				{
					string checkinComment = "Auto connected WebParts on page";

					// webparts are connected so update the page and check it in
					page.Update();
					page.CheckIn(checkinComment);

					if (publish)
					{
						page.Publish(checkinComment);
						page.Approve(checkinComment);
					}
				}
			}
			catch (Exception ex)
			{
				page.UndoCheckOut();
				throw ex;
			}
		}
	}
}

/// <summary>
/// Checks if a WebPart connection exists.
/// </summary>
/// <param name="connections">The connections.</param>
/// <param name="provider">The provider.</param>
/// <param name="consumer">The consumer.</param>
/// <returns></returns>
private static bool WebPartConnectionExist(Microsoft.SharePoint.WebPartPages.SPWebPartConnectionCollection connections,
	System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.WebPart consumer)
{
	foreach (Microsoft.SharePoint.WebPartPages.SPWebPartConnection conn in connections)
	{
		if (conn.Provider == provider && conn.Consumer == consumer)
		{
			return true;
		}
	}

	return false;
}

/// <summary>
/// Connects the web parts.
/// </summary>
/// <param name="manager">The manager.</param>
/// <returns></returns>
private static bool ConnectWebParts(Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager)
{
	bool connectionMade = false;

	// get the webparts on the page
	Microsoft.SharePoint.WebPartPages.SPLimitedWebPartCollection webparts = manager.WebParts;

	// only continu if there are any webparts
	if (webparts.Count > 0)
	{
		ProviderConnectionPointCollection provColl;
		ConsumerConnectionPointCollection consColl;
		// walk through all the webparts on the page to find provider webparts
		foreach (System.Web.UI.WebControls.WebParts.WebPart providerPart in webparts)
		{

			// check if the webpart is a Provider webpart
			provColl = manager.GetProviderConnectionPoints(providerPart);
			if (provColl != null && provColl.Default != null)
			{

				// walk through all the webparts on the page again now to find the consumer webparts
				foreach (System.Web.UI.WebControls.WebParts.WebPart consumerPart in webparts)
				{

					// first make sure the webpart isn't the provider webpart
					if (consumerPart != providerPart)
					{
						// check if the webpart is a Consumer webpart,
						// then make sure the consumer webpart isn't already connected to the provider webpart
						// then check if the interface of the consumer webpart is assignable from the interface of the provider webpart
						consColl = manager.GetConsumerConnectionPoints(consumerPart);
						if (consColl != null && consColl.Default != null
							&& !WebPartConnectionExist(manager.SPWebPartConnections, providerPart, consumerPart)
							&& consColl.Default.InterfaceType.IsAssignableFrom(provColl.Default.InterfaceType))
						{
							// connect the webparts
							manager.SPConnectWebParts(providerPart, provColl.Default, consumerPart, consColl.Default);
							connectionMade = true;
						}
					}
				}
			}
		}
	}
	return connectionMade;
}

Sample usage (console application)

try
{
	using (SPSite site = new SPSite("http://localhost"))
	using (SPWeb web = site.OpenWeb())
	{
		ConnectWebPartsOnPage(web, "pages/default.aspx", false);
	}
}
catch (Exception ex)
{
	Console.WriteLine();
	Console.WriteLine("Message:\n{0}\n\nStack:\n{1}\n\n", ex.Message, ex.StackTrace);
}

First NEN 2082 certified project with SharePoint 2010

March 4th, 2010 Jeffrey Tummers No comments


Together with 3 colleagues from QNH Business Integration I am currently finishing implementing a already NEN 2082 certified project based on SharePoint 2010.
When SharePoint 2010 hits RTM there will be another audit for this version of SharePoint, because currently the pre release version has been certified.

NEN 2082 is a Dutch standard which is similar the DoD5015 and MoReq standards.

This project was done for the Gemeente Nieuwegein, which is a Dutch local Government.

Gemeente Nieuwegein

For this project we have also been nominated for the “Innovation of the year” award:

LRG Innovator Nomination 2010

More information (Dutch)

My colleagues at QNH:

Categories: SharePoint Tags: , ,

Problem with VS2010 beta 2 Site Definition

December 16th, 2009 Jeffrey Tummers 3 comments

I encountered the following problem while creating a new custom Site Definition in Visual Studio 2010 beta 2.

The problem occurs when trying to create a new Document Library in the site based on the custom Site Definition.

Error while trying to add a new Document Library

Error while trying to add a new Document Library

Error message:

Error

An error occurred while getting items from the "" provider:
Cannot complete this action.

Please try again.

Read on to see the solution for this error.

Visual Studio 2010 now has native integration of SharePoint 2010 projects.

New Site Definition in Visual Studio 2010

New Site Definition in Visual Studio 2010

After creating the new Site Definition Project in the onet.xml file contains the following:

<?xml version="1.0" encoding="utf-8"?>
<Project Title="SampleSiteDefinition" Revision="2" ListDir="" xmlns:ows="Microsoft SharePoint" xmlns="http://schemas.microsoft.com/sharepoint/">
  <NavBars>
  </NavBars>
  <Configurations>
    <Configuration ID="0" Name="SampleSiteDefinition">
      <Lists/>
      <SiteFeatures>
      </SiteFeatures>
      <WebFeatures>
      </WebFeatures>
      <Modules>
        <Module Name="DefaultBlank" />
      </Modules>
    </Configuration>
  </Configurations>
  <Modules>
    <Module Name="DefaultBlank" Url="" Path="">
      <File Url="default.aspx">
      </File>
    </Module>
  </Modules>
</Project>

Now you can modify to the onet.xml and default.aspx as you like.

When deploying the new Site Definition, creating a site, and then trying to create a new Document Library you get the error above.  The solution for the error turned out to be the missing declaration of the <DocumentTemplates> XML node.

Correct default onet.xml should be:

<?xml version="1.0" encoding="utf-8"?>
<Project Title="SampleSiteDefinition" Revision="2" ListDir="" xmlns:ows="Microsoft SharePoint" xmlns="http://schemas.microsoft.com/sharepoint/">
  <NavBars>
  </NavBars>
  <DocumentTemplates>
  </DocumentTemplates>
  <Configurations>
      <Lists/>
      <SiteFeatures>
      </SiteFeatures>
      <WebFeatures>
      </WebFeatures>
      <Modules>
        <Module Name="DefaultBlank" />
      </Modules>
    </Configuration>
  </Configurations>
  <Modules>
    <Module Name="DefaultBlank" Url="" Path="">
      <File Url="default.aspx">
      </File>
    </Module>
  </Modules>
</Project>

After re-deploying the Site Definition and creating a new Site, you are able to create a Document Library.

Working new Document Library

Working new Document Library

Reference:

Microsoft’s Reference for SharePoint 2010 onet.xml

Looking for SharePoint 2010 Feature (GU)IDs

December 15th, 2009 Jeffrey Tummers 1 comment

Have you ever been looking for a feature (GU)ID in SharePoint? Well here is an easy way to find them.

I needed default SharePoint feature guids for the development of a custom Site Definition. Using a custom Site Definition you can automatically activate SharePoint features within the onet.xml file.

Here we go:

  1. Open the SharePoint site in IE8 (or IE7).
  2. Go to the Site Settings page.
    site-settings-menu
  3. Click on the Manage Site features (or Site Collection features)
    site-features
  4. Use the Developer Tools (shortcut F12)
  5. Click in the toolbar on Find –> Select Element by Click (shortcut CTRL+B, but this opens bookmarks manager on my machine)
    developertools-select
  6. Select the Activate (or Deactivate) button next to the feature you need the GUID from.
    select-feature-activate-button
  7. Developer Tools scrolls down to the HTML source for the specific button, the button tag is contained within a Div tag. This Div tag contains an ID attribute and thats the GUID for the feature!
    selected-feature-html
  8. In this case the GUID is: 9c03e124-eef7-4dc6-b5eb-86ccd207cb87
    selected-feature-guid

This trick also works for SharePoint 2007!