Hammendorp.net

Blogging about SharePoint and other development

Today i was trying to figure out how to set the option 'Start this workflow to approve publishing a major version of an item' on my SharePoint list workflow. After some research I found out that the way to go is this:

  1. Cast the SPList object to an SPDocumentLibrary
  2. Attach the workflow to the document library object
  3. Set the DefaultContentApprovalWorkflowId
  4. Update the list

The code will be something like this:

// list is an SPList object, workflowAssociation is an SPWorkflowAssociation object
SPDocumentLibrary docLib = (SPDocumentLibrary)list; // cast
docLib.WorkflowAssociations.Add(workflowAssociation); // attach
docLib.DefaultContentApprovalWorkflowId = workflowAssociation.Id; // set property
list.Update(); // save the list



Finally after lots of coding, testing and feedback; I have the new WSP listener ready. You can download it from codeplex.

In short, the improvements:

  • All assemblies and required assets in one WSP
  • Seperated code in library assembly
  • Activate the WSP Listener with one simple feature
  • Clear overview of (previous) installations in one document library
  • Several bugfix and code improvements

Hope you like it as much as I do.



I was making a simple console application to upload a zip file (with a WSP Listener solution) in the document library. This document library was located in the central admin, so I also wanted to find the central admin web application dynamic. I came up with the next solution:

// Set the path to the zip file and the name of the document library
string path = "c:\temp\testfile.zip";
string documentLibraryName = "WSPListener document library";

// Get the central administration web application and the upload folder
SPWeb centralAdminWeb = SPAdministrationWebApplication.Local.Sites[0].OpenWeb();
SPFolder uploadFolder = centralAdminWeb.Folders[documentLibraryName];
bool replaceExistingFiles = true;

SPFolder myLibrary = centralAdminWeb.Folders[documentLibraryName];

// Open a file stream to add the file in the document library
using (FileStream fileStream = File.OpenRead(path))
{
   SPFile spFile = myLibrary.Files.Add(fi.Name, fileStream, replaceExistingFiles);
   myLibrary.Update(); // save the file

   // Get the corresponding SPItem to update some properties and start the installation
   SPListItem item = spFile.Item;
   item["InstallationStartTime"] = null;
   item["InstallationEndTime"] = null;
   item["InstallationStatus"] = "Install";
   item["InstallationResult"] = "";
   item.Update();
}

As you can see, it's very easy :)



In my new update for the WSP Listener I need to save a file from a document library to disk. This can be done by the following code:

// item is an SPListItem
SPFile myFile = item.File;
DestinationFile = Path.Combine(@"c:\temp", myFile.Name);
if (File.Exists(DestinationFile))
{
   // Remove the old file
   File.Delete(DestinationFile);
}

// Open the binary data
byte[] data = myFile.OpenBinary();
using (FileStream stream = new FileStream(DestinationFile, FileMode.CreateNew))
{
   // Save the file
   BinaryWriter writer = new BinaryWriter(stream);
   writer.Write(data, 0, (int)myFile.Length);
   writer.Close();
   stream.Close();
}

Have fun!



Finally my first codeplex project is finished: http://wsplistener.codeplex.com/

Super easy copy paste installation of WSP files on different servers in your OTAP environments.

The WSP listener is a windows service application which waits for new config and WSP files in a specific folder. If a new config and WSP file are added, the WSP Listener will install the new SharePoint solution on the server based on values in de config file. The config file is a configuration file (for example MySharePointSolution.wsp.config) which defines the settings for the installation of the WSP solution file.

All feedback is welcome, hope this tool helps improvethe efficiency of your deployment



So I had this problem with updating site columns in existing site collections. I use a feature with element XML to install my custom site columns in the site collections. The problem is when I try to update some property of an existing site column, for example set required to TRUE, the update cannot be done by reinstalling the changed site column feature. After I reinstalled my updated site columns feature, no changes are comitted in the site columns of my site collection.

I tried ststadm -o deactivatefeature, then stsadm-o installfeature -force, ststadm -o activatefeature with some IISRESET's in between

So what is this hidden solution for this problem? Well the Field nodes in the element file has this strange property DisplaceOnUpgrade, set this to TRUE and the columns will be updated. The explanation from Microsoft:

DisplaceOnUpgrade: Optional Boolean. If a field definition already exists for the field, TRUE to force updates to field properties with the values that are specified in this field definition. Check the link for more info.

Hope this helps you!



So I was building my user control in SharePoint, no rocket sience I thought. When I opened my default.aspx page with the user control, the error 'could not load type ...' is shown. After some struggling and experimenting, I found out that my dll which was deployed in the GAC needed to be moved into the BIN folder.

Maybe there is a good explanation for this, but I have no clue ;) So my conclusion is to deploy the ASCX / ASPX code behind assemblies in the BIN folder of the web application.

Have fun!

Update 4 March 2010: Also when you get the error 'Could not load type ...' from a code behind file in for example a masterpage, try to move the assembly to the BIN folder



I was looking for an option to add the 'Draft Item Security' in the list schema file. This can be changed in the UI when you click the document library settings -> versioning settings. The possible options of 'Who should see draft items in this document library?' are:

  • Any user who can read items (DEFAULT)
  • Only users who can edit items
  • Only users who can approve items (and the author of the item)

If you want this setting automatically in you document library, you must add the DraftVersionVisibility attribute to your List node in the schema.xml. The value must be an integer which has the next options:

  • 1 = Only users who can edit items
  • 2 = Only users who can approve items (and the author of the item)
  • All other values = Any user who can read items (DEFAULT)

So for example :


 


<List Title="My new document list" DraftVersionVisibility="1">

Have fun!



Lat's say you want to show the results of your own document content type, but when you search all possible options show up. What you can do then is to add a rule to your search scope.

Go to site settings > Scopes > Click on your custom scope

The click on the 'add rule' option and in the next screen choose the 'Property Query' option. Then choose the 'contentclass' option in the 'Property Query' section. As a value for this property you must fill out the value STS_ListItem_<your list template ID>. For example if you used the template ID 10150 in your list feature, the contenclass must be equal to STS_ListItem_10150. The behaviour option must be set to Require.

After recrawling, the search result is narrowed to only your list items.



In a recent project I wanted to show two views of one list. The problem is that by default the title of the List node is copied into title[1] and title[2]. To solve this, you must include the <webpart> in the view element. So I opened the ONET.xml of the site definition. In the Configuration/Lists section I added my list feature:


<Lists>

 <List FeatureId="&lt;myGuid>" Title="My List" Type="12345" Url="Lists/MyList" />

</Lists>

Then in the Modules/Module/File section in the ONET.xml we need to add the View element:


<View List="Lists/My List" BaseViewID="1" WebPartZoneID="Header" WebPartOrder="1" Type="HTML">

 <![CDATA[

  <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">

   <Title>My first custom title</Title>

   <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

   <TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>

  </WebPart>

 ]]>

</View>


<View List="Lists/My List" BaseViewID="2" WebPartZoneID="Header" WebPartOrder="2" Type="HTML">

 <![CDATA[

  <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">

   <Title>My second custom title</Title>

   <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

   <TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>

  </WebPart>

 ]]>

</View>

As you can see, in the CDATA you can find an element Title which you can use for your own custom title!

Have fun!



Bas Hammendorp

I am Bas Hammendorp, working as SharePoint developer at PGGM Zeist

Month List

Sign in