2009-07-17 53 views
0

我需要帮助创建一种方法,以便在特征激活期间创建一堆字段。 C#。大约有15个不同类型的字段,我希望能够通过所有必要的属性来创建每个字段。在特征激活中以编程方式创建字段

任何人都有关于此的任何示例代码或指导?

回答

0

好吧,我找到了答案的一部分...相当多的去虽然。我发现下面的实用方法here

public void AddCustomField(SPWeb web, string fieldType, string fieldName, bool isRequired, string defaultValue, string fieldGroup) 
    {    
     //Check if the field is there or not already    
     if (!web.Fields.ContainsField(fieldName))    
     {     
      //Initializing a SPField instance     
      SPField customField;     
      //Creating a new filed     
      customField = web.Fields.CreateNewField(fieldType, fieldName);     
      //Assigning a group     
      customField.Group = fieldGroup;     
      //Sets this field is required field     
      customField.Required = isRequired;     
      //Assigning a default value     
      customField.DefaultValue = defaultValue;     
      //Adding the newly created field to SPweb     
      web.Fields.Add(customField);    
     }   
    } 

但是,我不知道如何调用该方法,可能有人给我一个例子吗?