Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to create a site column using via a console app. I would ultimately like to be able to migrate the site column and upgrade the same column via this app. To that end, I would need some control over the column's GUID. Is it possible to set GUID using the object model for a site column. I could not get a clue from the API: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield_members.aspx

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Yes, you can specify it. Example:

public static readonly Guid MyFieldId = 
  new Guid("{CC1E421C-29BE-4373-81D0-55D5D64B2E3D}");
public static readonly string MyFieldDefXml =
  "<Field ID=\"{CC1E421C-29BE-4373-81D0-55D5D64B2E3D}\"" +
  " Name=\"MyFieldName\" StaticName=\"MyFieldName\"" +
  " Type=\"Text\" DisplayName=\"My Field Name\"" +
  " Group=\"My Columns\" DisplaceOnUpgrade=\"TRUE\" />";

if (web.AvailableFields.Contains(MyFieldId) == false)
{
  web.Fields.AddFieldAsXml(MyFieldDefXml);
}

More information: http://msdn.microsoft.com/en-us/library/ff798370.aspx#Y209

share|improve this answer
2  
Main point here is that you can only do it via XML - so either declaratively in a Feature, or as show here, using the AddFieldAsXml method. –  James Love Jul 5 '11 at 20:30
    
That's right, James –  Ashish Patel Jul 5 '11 at 20:34
    
Ashish, Thank you. I will try this out. But should the Field "ID" take the value of "MyFieldId" in the code above? –  Sam Patrick Jul 5 '11 at 20:46
    
It takes only a valid GUID –  Ashish Patel Jul 5 '11 at 20:49
    
@Ashish I meant to say: should CC1E421C-29BE-4373-81D0-55D5D64B2E3D be replace by 891B57CF-B826-4B0C-9EDF-8948C824D96F in the above code? I see that ID of MyFieldDefXml is not really being checked. Or may be I am missing something? –  Sam Patrick Jul 5 '11 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.