Property promotion/demotion between file and columns in SharePoint – Part 2

In a previous post I showed you how to do property promotion/demotion through SPD. This time around though I´m going to use a feature to accomplish the same thing, which I promised to do.

So first of all, why use a feature?
Well, it is best practice to package your “stuff” into features and WSPs and of course I wan´t my stuff to be source controlled and that is not something that SPD is famous for handling very well.
Besides, I probably wan´t this “feature” to be installable in more one environment (I usually don´t develop on the production server).
And, lastly…I don´t like SPD, it´s not a tool for developers.

OK, with that said, let´s get to work.

1. Start with creating feature to provision Content Type/Site Column.

An excerpt of the elements.xml file looks like this:

<ContentType
ID="0x010101008103BAF1CBB541CB9F9A6DF623AEFEDD"
Name="Book"
Group="JL"
Description="Create a new book">
 

<!--<FieldRefs> REMOVED DUE TO SPACE-->   

</ContentType>

<Field
ID="{f17f0fc3-f240-4a54-9736-5fa9a0fe0a26}"
Name="ISBN"
DisplayName="ISBN"
Type="Text"
Node="Book/ISBN"
StaticName="ISBN"
DisplaceOnUpgrade="TRUE"
Group="JL" xmlns="http://schemas.microsoft.com/sharepoint/" />

Note:
The contenttype inherits from Form.
The Node attribute is where I put the XPath that SharePoint will use to find the property in the xml. So the content in the xml files that I upload into this doc lib should look something like this in order for the XPath to work:

<Book>
    <ISBN></ISBN>
</Book>

 

2. Create an instance of a form library that includes the content type.

I usually like to create a custom list template with a minimal schema.xml and then customize my list instance in a feature receiver, which I have blogged about before.
Although, with this simple setup you wouldn´t actually need to create a custom template, using the built in forms lib would work just fine. 

The elements.xml file includes the list template and in the feature receiver code I´m adding an instance of this list template. All steps are documented in a previous post about list templates. 

This is the code for the feature receiver (see the previous post for an explanation of the code):

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
 
 

SPWeb web = properties.Feature.Parent as SPWeb; // no dispose required

if (web == null)
{

return;

}

SPList instance = web.FindListByName("Books");

if (instance == null)
{

SPListTemplate template = web.FindListTemplateByName("Books");

if (template != null)
{

Guid id = web.Lists.Add("Books", "List containing books", template);
instance = web.Lists[id];

}

}

 

instance.AddContentType(new SPContentTypeId("0x010101008103BAF1CBB541CB9F9A6DF623AEFEDD"));
instance.RemoveContentType(SPBuiltInContentTypeId.Item);
instance.RemoveContentType(SPBuiltInContentTypeId.XMLDocument);

instance.CustomizeDefaultView(new Guid[]

{

SPBuiltInFieldId.LinkFilename,
new Guid("69a5f580-2e33-4bdc-87e2-0888e20e3046"),
new Guid("f17f0fc3-f240-4a54-9736-5fa9a0fe0a26")

});

}

So, when I have activated these features the Books library shows up in SharePoint.
You could use this library in two different ways in order for the promotion/demotion to work.

Either you “upload” files to it by code by using an empty (template) xml file and adding metadata to the SPListItem or by supplying a HashTable to the RootFolder.Files.Add method.
The other option is to always upload files that meet the specified schema of nodes.

I will try out my new form lib by uploading a file that meets the schema I have set up.

 So, when I upload files I´m asked to fill in some values.

 

 I’ll  upload three files to my library. 

I have tried changing values both in the xml and in the metadata. The promotion/demotion works perfect.

 

Great!

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: