Wednesday, February 13, 2013

Declare Calculated type field in content type

I had a requirement to create a calculated field in the content type to combine document id and generation id into full document id. The twist was to format the ID and Generation into the form 00000-00 (i.e. padding additional 0's)


<Field ID="{979A0DF9-4FD2-4A15-A459-19B351479067}" Name="FullDocumentID" DisplayName="Full DocumentID" Type="Calculated" ResultType="Text" Required="FALSE" AllowDeletion="FALSE" ShowInDisplayForm="TRUE" ShowInEditForm="FALSE" ShowInNewForm="FALSE">
     <Formula>=CONCATENATE(TEXT([DocumentID],"00000"),"-",TEXT( [Generation],"00"))</Formula>
    <FieldRefs>
      <FieldRef ID="{5EF364B0-908D-44E8-BFBC-E392539B68D2}" Name="DocumentID"/>
      <FieldRef ID="{E4464EBA-6959-4FB4-B7AB-F722B8290C82}" Name="Generation"/>
    </FieldRefs>
  </Field>

populate logged in user in user type field

My content type had a field of type user
<Field ID="{7F6FA252-755B-4062-97E6-01618181968A}" Name="DocumentAuthor" DisplayName="Author" Type="User" List="UserInfo" Required="FALSE" EnforceUniqueValues="FALSE" UserSelectionMode="PeopleOnly" UserSelectionScope="0" />

customer wanted to populate the current user by default. I made use of list event receiver as below

public override void ItemAdding(SPItemEventProperties properties)
{
 //set author field to current user by default
SPWeb web = properties.Web;
SPListItem curDocument = properties.ListItem;
SPUser usr = web.CurrentUser;
SPFieldUserValue defValue = new SPFieldUserValue();
defValue.LookupId = usr.ID;
properties.AfterProperties["DocumentAuthor"] = defValue.ToString();
base.ItemAdding(properties);
}

This page is powered by Blogger. Isn't yours?