Set Default Value of column in document library after/In Checked In event in Sharepoint

Object: When a file is uploaded in document library, then "NewColumn" must contain a value that depends on other column value. In other words, On Meta property depends on other property.
Solution:
In general case, Check out is required for updating property. If you use "Checking in" event for this then you will get error
"


Save Conflict: Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes."
So use following code:

public override void ItemCheckedIn(SPItemEventProperties properties)

{

base.ItemCheckedIn(properties);

//Set NewColumn value = Other Column + ID

using (SPWeb mySite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))

{

try

{

mySite.AllowUnsafeUpdates =true;

SPFile file = properties.ListItem.File;

file.Update();

properties.ListItem["NewColumn"] =properties.ListItem["OtherColumn"].ToString()+ properties.ListItem["ID"].ToString();

properties.ListItem.SystemUpdate();

}

catch (Exception ex)

{

mySite.Description = ex.ToString();

}

mySite.Update();

}

}



2 comments:

  1. Um excuse my ignorance...im new to sharepoint as ive been lumped with it from its predecessor. Where do I put this code??

    ReplyDelete
  2. Hi Belle,

    See following URL:
    http://urenjoy.blogspot.com/2008/10/how-to-handle-event-using-net-in-moss.html

    ReplyDelete