Thursday, December 11, 2008

STSAdm Case Sensitivity

One thing to be aware of when deploying a site collection feature using STSAdmin is that is that the -URL parameter is CaSe SenSitivE.
If you don't follow this, you might get in trouble just as I did.

STSAdm.exe -o installfeature -filename "HR_MetaData\Feature.xml"

STSAdm.exe -o activatefeature -filename "HR_MetaData\Feature.xml" -url "http://MySite/sites/humanresources"

The error message:
"Unknown Error"
If you have turned off custom Errors in Sharepoint you should see the following error:

Object reference not set to an instance of an object.
at Microsoft.SharePoint.ApplicationPages.FieldListRenderer.Render(HtmlTextWriter output)
...
...
You will see the errors above when navigating to Site Actions -> Site Settings -> Site Columns;
or navigate to: http://MySite/sites/HumanResources/_layouts/mngfield.aspx

Resolution
When you deploy a feature using STSAdm -url make sure you take into consideration the case of the URL.

After using the correct case everything worked beautifully.

STSAdm.exe -o activatefeature -filename "HR_MetaData\Feature.xml" -url "http://MySite/sites/HumanResources"

Monday, November 17, 2008

Hide or Remove Title and other SharePoint built-in fields from a Content type using XML

I've created a feature to deploy my own SharePoint fields and Content types.
My custom Content types inherits from either Item or Calendar and I was looking for a way to hide or Delete the standard fields "Title" for Item and "Description", "Event Date" and "End Date" for Calendar.
My objective was to hide these fields from users when they access SharePoint's EditForm.aspx and NewForm.aspx.

Here is how I've done it:
In the Content Type definition XML you first need to remove the field using removefieldref, then add it again with the hidden tag set to true.


<contenttype id="0x010092BE5A7A635CC84081F8D4F32D3A52CD" name="Customer" group="MyGroup">
<fieldrefs>
<removefieldref id="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" name="Title">
<fieldref id="fa564e0f-0c70-4ab9-b863-0177e6ddd247" name="Title" hidden="TRUE">

<fieldref id="20d446ef-0e0c-404f-ab4b-905732e367df" name="CustomerAddress" >
<fieldref id="0e6b89c0-494a-4b0b-8fb0-134dd3752282" name="CustomerName">
</fieldref>
</fieldref>
</fieldref></removefieldref>
</fieldrefs></contenttype>


Same can be applied to other SharePoint system fields (e.g. "Event Date", "Description", etc).

Whether this is a bug or standard behaviour I don't know, but from my experience you will need to use both "removefieldref" and then add the field again (fieldref) with tag hidden="TRUE". By using only one of the two it simply doesn't work. You can achieve the same results by using SharePoint UI or the programatically using the Object Model.


To get up to speed with Content Type deployment, refer to Microsoft SDK Documentation.

Hope this helps!

Tuesday, September 23, 2008

How to modify a DateTime value in SharePoint AfterProperties

When you change a DateTime field value in SharePoint's SPItemReceiver AfterProperties/BeforeProperties you need to set it to a specific DateTimeFormat string;

The Scenario is the following:

  1. Created a SharePoint Customer List with a number of fields, including Cancelled (checkbox) and DateCancelled (DateTime readonly);

  2. Created a .Net Assembly with the business logic. The business logic is simple: If user ticked the Cancelled checkbox, the DateCancelled field would be populated with the current TimeStamp;

  3. Created a Feature Receiver to attach the assembly to the List - See SDK documentation for detailed instructions on how to set this up;


My first attempt:



class EventHandler : SPItemEventReceiver
{
public override void ItemUpdating(SPItemEventProperties inProperties)
{
base.ItemUpdating(inProperties);
.....
if (conditions)
{
inProperties.AfterProperties["DateCancelled"] = DateTime.Now;
// This also does not work
inProperties.AfterProperties["DateCancelled"] = DateTime.ToLongDateString();
}
}


The error message:
Invalid date/time value
A date/time field contains invalid data. Please check the value and try again.


My Solution:
After many attempts and some googling, I found out that when setting a DateTime value in AfterProperties you have to convert the the DateTime to an ISO8601 string format - I could not find any official documentation explaining the reason for this.
So here is how I've solved this:


if (conditions)
{
inProperties.AfterProperties["DateCancelled"] = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);
// This also works :
inProperties.AfterProperties["DateCancelled"] = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ssZ");

}



Hope this helps.

Friday, September 19, 2008

Yet another blog

My desire to create a technical blog has been around for a while but I have been putting it off cause Internet is already full of great, complete,official, accurate, excellent blogs.
A few months back I started developing apps for SharePoint, and, for some issues I came accross I couldn't find much information out there so I thought it would be helpful to create this space and share with you my experiences and discoveries in the SharePoint world.

I hope you enjoy!