Home > Deveploment and Programming, SharePoint Development > How to update properties of existing Custom Site Column in C#

How to update properties of existing Custom Site Column in C#

Hi,

Some time you may required to update property of existing custom site column like need to convert type of column or many thing can be possible, there are many way to do the same, see the simplest way to do pro grammatically in C#.

Just you need to write simple line of Code, you will see the change after executing the below code.

using (SPSite site = new SPSite(“http://localhost:5000”))
{
using (SPWeb web = site.OpenWeb())
{

SPField fa = web.Fields[“Document Approver”];
fa.SchemaXml = fa.SchemaXml.Replace(“<Field”, “<Field List=\”UserInfo\” “);
fa.PushChangesToLists = true;
fa.Update();

}
}

There is an custom field with User type named “Document Approver”, the above code is adding one property “List=UserInfo”.

Before updation the schema xml of custom field,

<Field ID=”{b81d6548-115e-43b8-8745-e7ba162c893c}” Type=”User” Name=”Document_Approver”
StaticName=”Document_Approver” DisplayName=”Document Approver”
Required=”FALSE” Group=”custom” SourceID=”{1a517130-6e88-4412-ae5e-b79c6b6b1f32}” Version=”2″ />

After updation of schema xml of custom field,

<Field List=”UserInfo” ID=”{b81d6548-115e-43b8-8745-e7ba162c893c}” Type=”User” Name=”Document_Approver”
StaticName=”Document_Approver” DisplayName=”Document Approver”
Required=”FALSE” Group=”custom” SourceID=”{1a517130-6e88-4412-ae5e-b79c6b6b1f32}” Version=”2″ />

Do let me know, in case of any query.

Cheers

Sanjiv Kumar

Advertisements
  1. CBarkers
    10/07/2013 at 10:01 pm

    Hi, thank you for this beautifull tips.

    May I ask you a question about changing the SourceID.?

    I’m actually on a project that contains field created with VS but with no SourceID, I would like to replace the automatic source ID depending on list with generic source id type of : SourceID=”http://schemas.microsoft.com/sharepoint/v3″

    Do you think there is a way to do this without loosing all documents and datas?

    Thanks for your time

    Regards
    CBarkers

  2. CBarkers
    11/07/2013 at 3:12 pm

    Using your tips, i’ve done script powershell that chance source ID without any lost

    Really good job and thx

    Regard
    CBarkers

  3. 03/10/2013 at 11:57 pm

    string fullfieldname = ConfigurationSettings.AppSettings[“FullFieldName”];
    string url = ConfigurationSettings.AppSettings[“URL”];
    SPSite site = new SPSite(url);
    SPWeb web = site.OpenWeb();

    SPField orderableConfirm =(SPField) web.Fields[fullfieldname];
    orderableConfirm.Description = “Hello”;
    orderableConfirm.PushChangesToLists = true;
    orderableConfirm.ShowInEditForm = false;
    orderableConfirm.Update();
    web.Update();
    web.Dispose();

    Issue with this code is it only pushing description to all list but not pushing
    orderableConfirm.Description = “Hello”; ( pushing)
    “orderableConfirm.ShowInEditForm = false;” ( not pushing )

    Any help will be appreciated !!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: