Wednesday, May 30, 2007

Deploying a calculated site column using a feature in SharePoint 2007

It is not very easy to find a sample of how to define a site column of type calculated using the SharePoint 2007 features framework. The documentations shows how to do it using the user interface (see Calculate data in lists or libraries ). The documentation in the WSS SDK about the  Formula Element in the CAML Core Schemas points in the right direction but is maybe not clear enough. I tested the sample code below by using the site columns in a list definition. You might want to test it out in a content type as well - please provide feedback in the comments.

Code sample feature.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Scope="Site" Title="Calculated Sitecolumn" Id="49C90DCA-6C76-4b30-99DB-7C4C7D100761">
<ElementManifests>
<ElementManifest Location="calcsitecolumn.xml"/>
</ElementManifests>
</Feature>

Code sample calcsitecolumn.xml

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field Name="CalcDemo" StaticName="CalcDemo"  Type="Calculated" DisplayName="Calculated column" ID="{DD01AF0E-5E58-4e50-B3E8-1BFC866CA9A0}" ResultType="DateTime" Format="DateOnly" ReadOnly="TRUE">
<Formula>=StartDate+7</Formula>
<Default></Default>
<FieldRefs>
<FieldRef Name="StartDate" ID="{64cd368d-2f95-4bfc-a1f9-8d4324ecb007}" />
</FieldRefs>
</Field>
</Elements>

Have fun with it ...

 

 

 

6 comments:

Anonymous said...

Is it possible to create a custom formula that could be referenced by a calculated field?

For example, would it be possible to create a formula called ReplaceAll which would replace all instances of a sub-string in a string with another sub-string.

Any thoughts??

Thanks,
Saurabh

Komakino said...

I was looking for a solution like this. I know my mistake now. Thanks for sharing ;)

Sachin said...

Can you share the demo as i have done same thing as per your demo but i am not able to see my site column in site column gallary

Anil Kumar said...

Hi,

I trying to create feature to deploy or manage a site column which is derived from a custom field type. But the problem here is the Custom field type takes a string parameter called MetadataType. I need to pass it to the custom field type to create a site column, But couldnt find any resource how to go about it.
How to pass a value to the field type property

Thanks in advance

Unknown said...

Try this :





=DATE(YEAR(Created),MONTH(Created),DAY(Created))







1
2
3




=DATE(YEAR(dueDate)+[RetentionPeriod],MONTH(dueDate),DAY(dueDate))
















Unknown said...





=DATE(YEAR(Created),MONTH(Created),DAY(Created))







1
2
3




=DATE(YEAR(dueDate)+[RetentionPeriod],MONTH(dueDate),DAY(dueDate))