Friday, December 9, 2011

Blocking updates for document library

Scenario:
For one of the new requirement we needed to block all updates to documents in document library

1. User should able to upload a new document
2. User should not be able to upload a document with the same file name as existing document
3. User should not be able to edit metadata once it has been entered.

Solution:
Simple event receiver did the trick but as always with some learning / gotta's.

Assuming 2 different situations
1. Simple document library with no extra metadata columns : In this we will need ItemAdded event to update the event once.

2. Document library with extra properties : We dont need ItemAdded event and code can be commented.

Code:

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

namespace SKN
{
class EventHandler1 : SPItemEventReceiver
{
public override void ItemAdded (SPItemEventProperties properties)
{
base.ItemAdded(properties);

//DisableEventFiring();
//properties.ListItem.Update();
//EnableEventFiring();
}

public override void ItemUpdating (SPItemEventProperties properties)
{
base.ItemUpdating(properties);

if (!string.Equals(properties.ListItem["Created"], properties.ListItem["Modified"]))
{
properties.Cancel = true;
properties.ErrorMessage = "no update";
}
}
}
}

0 comments: