Archive for the ‘Microsoft Sharepoint Server 2010’ Category

Versioning and upgrading your features with SharePoint 2010 Application Lifecycle Management (ALM)

30. April 2010

One of the nice features of SharePoint 2010 is the possibility you actually have to version your features and update your features in a more sensible way than the previous version did. In SharePoint 2007 the feature had a version property, which basically allowed developers to keep track of the versions themselves – SharePoint did in other words never use the property for anything.

Times have changed – in SharePoint 2010 the application Lifecycle Management actually allows you to upgrade your features which in the simplest form lets you change the name of previous uploaded files, add an additional element-file or the thing I have seen cause the most WAUW-effect during the Ignite-trainings I have held in Denmark – adding fields to an already deployed content type in your farm. In this walk-through our goal is to accomplish the last scenario – so let’s start up. I have a newly created team-site with no customizations accomplished on the url http://intranet

Creating the basic content type feature

So let’s start up by creating a basic feature with a content type with a single note-field – just to have something to play around with

  1. Open Visual Studio 2010 and create a new Empty SharePoint Project – my project is named TheUpgradeableCT – and the name will be important later, so if you chose another name, some of the walkthrough won’t be completely copy/paste. Deploy it as a farm solution against http://intranet or whatever site collection you wish as a debug-site – again – I will use the url later
  2. Right-click the project and add a Content Type Item to the project – name it The Upgradeable Content Type and chose Document as the base content type when the wizard appear and click Finish.
  3. Paste the following into the elements-file created in your content type node – this will create a basic document content type

<?xml version=1.0encoding=utf-8?>
<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

<!– Parent ContentType: Document (0×0101) –>
<ContentType ID=0x0101009214903b71c048a0ba7a7ec3dff0d9dd
Name=TheUpgradeableCT
Group=A feature upgrade attempt
Description=The upgradeable content type
Version=0>
<FieldRefs>
<FieldRef ID={67BC24C5-C864-4CE5-9855-B515EEF54773}/>
</FieldRefs>
</ContentType><Field ID={67BC24C5-C864-4CE5-9855-B515EEF54773}
Name=Note1
StaticName=Note1
DisplayName=Note on version 1
Type=Note
Group=A feature upgrade attempt/>
</Elements>


  1. Change the version number of your feature by double clicking the feature node and change the feature version to 1.0.0.0 in the Properties window of your feature

  2. Deploy your solution and check that your content type is deployed successfully – and create a document library and attach the content type to it in the UI.

Make the extra field for the Content Type for version 2.0.0.0

For version 2.0.0.0 we will add an additional custom field for our content type and make the initial configuration of our feature ready for upgrading to the new verison, so let’s hit it by adding a new Field Item to our Sharepoint project and package the project to make it ready for the upgrade-celebration:

  1. Right-click the project-node of your Visual Studio Project and add an Empty Element called Version2Fields. It contains an element.xml, in which you put the following field, which will be the additional field of our feature in version 2.0.0.0:

<?xml version=1.0encoding=utf-8?>
<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<Field ID={67BC24C5-C864-4CE5-9855-B515EEF54774}
Name=Note2
StaticName=Note2
DisplayName=Note on version 2
Type=Note
Group=A feature upgrade attempt/>
</Elements>

  1. Expand your feature node untill the template.xml appears…open it. This is basically your feature without all the properties in it. Place your cursor between the open and close feature tag and add an UpgradeActions element with a corresponding closing element.
  2. In the UpgradeActions place a VersionRange to tell the versions you are upgrading from and to. Give it the properties BeginVersion=”1.0.0.0″ and EndVersion=”2.0.0.0″.
  3. Between the versionrange you can specify what will happen when we are upgrading from version 1 to 2. In our case we will add an additional elementfile, so insert a ApplyElementManifests and put a ElementManifest inside here with a location of your additiona element file
  4. Furthermore we need to add this field to the content type we just made – so add an AddFieldToContentType and give it the id of the content type and the field and specify whether to pushdown the field to all content types inheriting from the specified content type. Your Feature file should look something like this

<?xml version=1.0encoding=utf-8 ?>
<Feature xmlns=http://schemas.microsoft.com/sharepoint/>
<UpgradeActions>
<VersionRange BeginVersion=1.0.0.0EndVersion=2.0.0.0>
<ApplyElementManifests>
<ElementManifest Location=Version2Field/Elements.xml/>
</ApplyElementManifests>
<AddContentTypeField
ContentTypeId=0x0101009214903b71c048a0ba7a7ec3dff0d9dd
FieldId={67BC24C5-C864-4CE5-9855-B515EEF54774}
PushDown=TRUE/>
</VersionRange>
</UpgradeActions>
</Feature>

  1. Double-click your feature and change the version to version 2.0.0.0
  2. Right click your project and chose Package to package your solution

Upgrade your solution and feature in the farm

To upgrade your solution in the farm can be done via stsadm, custom code or PowerShell – and since one of the coolest parts of Sharepoint 2010 is Powershell, find your favorite PowerShell Editor (I User PowerGUI) and fire the following Powershell script with the path to your solution and the name of your solution

Update-SPSolution -Identity TheUpgradeableCT.wsp -LiteralPath C:\devprojects\TheUpgradeableCT\TheUpgradeableCT\bin\Debug\TheUpgradeableCT.wsp –GACDeployment

This alone isn’t enough to upgrade it – you to call a certain method to upgrade your feature – and this can be done via psconfig or custom code in any way you prefer….and I prefer the code attempt, so fire up a new instance of Visual Studio 2010 and make yourself a Console application (remember to change it to 64 bit build and reference Microsoft.SharePoint.dll):

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

namespace UpgradeTheContentType
{
class Program
{
static void Main(string[] args)
{
using (SPSite currentSite = new SPSite(“http://intranet”))
{
foreach (SPFeature feature in currentSite.QueryFeatures(SPFeatureScope.Web,true))
{
if (feature.Upgrade(false) != null)
{
Console.WriteLine(“Feature “ + feature.DefinitionId.ToString() + ” failed to upgrade!”);
}
else
{
Console.WriteLine(“Feature “ + feature.DefinitionId.ToString() + ” was successfully upgraded!”);
}
}
}
}
}
}

This bit of code queries every feature at the web scope at http://intranet for features in need of an update and actually tries to update them

So that’s all…check your content type and your list instance for the new field – which should be present. In the next post I will make the FeatureUpgrading of the FeatureReceiver a bit clearer. Until then the code is available at my DropBox at  http://dl.dropbox.com/u/6050844/Blog/UpgradeContentTypes.zip

Have fun

Peter

PowerShell and Sharepoint 2010 – the introduction

6. March 2010

Powershell is a new addition to SharePoint 2010 – and to spend a few hours becoming familiar with the PowerShell commands will definitely save you a minute or 10 now and then in your daily life with SharePoint 2010. In this first post, we will get in touch with PowerShell and start out with a few “Hello World of PowerShell for Sharepoint” scripts, and the later – if I hopefully find the time – I wil make a deeper dive into this world. So let’s hit it – let’s SharePoint….!!!!

First of all we need a fancy command prompt – to get it click Start | Run and in the box type cmd and click OK – and BAM – you have a nice command box ready for use. To make it a powershell command type powershell and press enter

To communicate with the host(i.e making the prompt write information for you) you have to use the command Write-Host. So typing Write-Host Hello World at the prompt will make it send you a message. Actually you can format it very basically. –foregroundcolor green will eg. Make the text green and you also have a backgroundcolor you can play around with. If ever in doubt what parameters a certain powershell-command takes write get-help and the name of the help function will help you.

This is very basically what you needs of information of PowerShell to get rolling with PowerShell for SharePoint 2010 – so let’s hit it and get some information regarding SharePoint in the box. To get to that point we need some extra information regarding Powershell – and that is that the functionality you can use is depending on what snap-ins you have loaded. A snap-in is no more than a bunch of functionality in one package – and we need to load the ones for SharePoint – to do that type Add-PSSnapin Microsoft.SharePoint.PowerShell at the command prompt.    

To get a list of commands available type Get-command and nearly everything which includes an SP will be sharepoint-related. However, this is quite a few –so to get a document with all of them, type get-command –pssnapin Microsoft.sharepoint.powershell > c:\commands.txt and a nice notepad-doc will be created for you at the c-drive including all of the commands available.

A lot of gets and sets are present – so try to write Get-SPWebApplication at the prompt – and notice that a list of Web Applications with DicplayName and Urls from the farm will appear. Nice And easy J

Basically there are two important kind of PowerShell commands for SharePoint – the ones starting with GET-SP – which gets you something and NEW-SP which creates something. If you want to create a new WebApplication this can be done with the New-SPWebApplication command – so try to type Get-Help New-SPWebApplication at the command prompt to see what is needed for a new Web Application – the following is returned

So typing New-SPWebApplication –name “Qwert Intranet” –ApplicationPool “Qwert Intranet” –ApplicationPoolAccount q\administrator –DatabaseName SharePoint_Qwert_Content_Intranet –HostHeader Intranet.qwert.dk –port 80 –url http://intranet.qwert.dk

After thinking a while – your web application is created – confirm by writing Get-SPWebApplication at the command prompt and your new web application will appear.

Deleting the web application is providing by Remove-SPWebApplication – try typing get-help Remove-SPWebApplication to read more about the properties – the command below deletes your web application – note that you have to confirm the action.

A final Get-SPWebApplication will confirm the deletion.

Enabling the Developer Dashboard in Sharepoint 2010

23. December 2009

Introducing the Developer Dashboard in Sharepoint 2010, developers will get the tool they were looking for: A dashboard saying how long time the custom code took to execute, what queries were ran at the sql server and a lot of other fun stuff…..only problem…..it pro’s and end users doesn’t care about it, and therefore it is disabled by default – and there is no button the enable and disable it.

How is it done then…..there are three options, stsadm, PowerShell or custom code. In this blog-post we will be dealing with the code-option

The code-attempt

Open Visual Studio 2010, create a new Console Applicaton and make sure the .Net Framework 3.5 is chosen in the top…..SHarepoint has no idea of what .Net Framework 4.0 is.

Change the build setting of the project from x86 to any CPU in the project properties and save them.

Include the namespace of sharepoint administration by pasting the following just beneath the other using-statements:

using Microsoft.SharePoint.Administration;

And finally paste the following lines of code in your Program.cs files in static void main

SPWebService contentService = SPWebService.ContentService;

contentService.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.On;

contentService.DeveloperDashboardSettings.Update();

This will enable the developer dashboard for the entire farm – and it will look something like this:

Have fun J

List form from Infopath 2010 is blank

22. December 2009

Well…..one of these you use a couple of hours to solve and looking back you know it should only have lasted 20 minutes, if your brain had been pressent :-)

Here we go….when creating a new list item in SHarepoint 2010 in a list with a list form edited in Infopath 2010 you get a pop-up with no fields and no headings – even though you know you have placed several fields in it.

The trick is called “System State” and is the same as in Sharepoint 2007 where you got it from the Shared Service Provider, but since there is no Shared Service Provider in Sharepoint 2010 you need to find it another place.

Open the Central Administration and click on configuration wizard in the quick launch. Start the configuration wizard and make sure System State is checked on the services page. After 5 minutes every Infopath form rendered like a dream.

Actually there is another way of doing this, if you are not the “wizard-kind-of-Sharepointer” and this is through PowerShell – I have searched but haven’t found a “Enable Session State” in the Central admin. The cmdlets are Get-SPSessionStateService, which returns a true or a false indicating if it is enabled or not – and if it is not, you can enable it with the cmdlet Enable-SPSessionStateService, which takes a lot of parameters – and the only mandatory is Databasename.


Follow

Get every new post delivered to your Inbox.