学习:Dynamically Creating Sharepoint Content Type (转)
2011-09-27 15:08
489 查看
In a scenario where you need to create Content Type in bunch, there comes the role of Dynamic Content types.
The following code snippet helps you in creating content type dynamically.
Code:
using System;
using Microsoft.SharePoint;
namespace DynamicContentTypeConsole
{
class Program
{
static void Main(string[] args)
{
string siteURL = "URL of Site";
using (SPSite objSPSite = new SPSite(siteURL))
{
using (SPWeb objSPWeb = objSPSite.OpenWeb())
{
SPContentType objSPContentType = new SPContentType(
objSPWeb.AvailableContentTypes["ParentContentType"], objSPWeb.ContentTypes, "Dynamic Content Type");
// A Required Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic Number Column", SPFieldType.Number, true);
SPFieldLink objSPFieldLink1 = new SPFieldLink(objSPWeb.Fields["Dynamic Number Column"]);
objSPContentType.FieldLinks.Add(objSPFieldLink1);
// A required String Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic String Column", SPFieldType.Text, true);
SPFieldLink objSPFieldLink2 = new SPFieldLink(objSPWeb.Fields["Dynamic String Column"]);
objSPContentType.FieldLinks.Add(objSPFieldLink2);
//A non-required Choice Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic Choice Column", SPFieldType.Choice, false);
SPFieldChoice choicefield = (SPFieldChoice)objSPWeb.Fields["Dynamic Choice Column"];
// Add a group to the filed
choicefield.Group = "Dynamic Group";
// Add choices
choicefield.Choices.Add(string.Empty);
choicefield.Choices.Add("Yes");
choicefield.Choices.Add("No");
// Set the default choice
choicefield.DefaultValue = string.Empty;
choicefield.Update();
SPFieldLink objSPFieldLink3 = new SPFieldLink(choicefield);
objSPContentType.FieldLinks.Add(objSPFieldLink3);
objSPWeb.Fields.AddLookup("Dynamic Lookup Column", new Guid("GUIDofRemoteList"), false);
SPFieldLookup lookupfield = (SPFieldLookup)objSPWeb.Fields["Dynamic Lookup Column"];
// Set the remote field
lookupfield.LookupField = "RemoteFieldName";
lookupfield.Update();
SPFieldLink objSPFieldLink4 = new SPFieldLink(lookupfield);
objSPContentType.FieldLinks.Add(objSPFieldLink4);
objSPWeb.ContentTypes.Add(objSPContentType);
objSPContentType.Update();
}
}
}
}
}
The code snippet is the Console Application, and is ready to use.
Come from: http://www.directsharepoint.com/2011/04/dynamically-creating-sharepoint-content.html
The following code snippet helps you in creating content type dynamically.
Code:
using System;
using Microsoft.SharePoint;
namespace DynamicContentTypeConsole
{
class Program
{
static void Main(string[] args)
{
string siteURL = "URL of Site";
using (SPSite objSPSite = new SPSite(siteURL))
{
using (SPWeb objSPWeb = objSPSite.OpenWeb())
{
SPContentType objSPContentType = new SPContentType(
objSPWeb.AvailableContentTypes["ParentContentType"], objSPWeb.ContentTypes, "Dynamic Content Type");
// A Required Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic Number Column", SPFieldType.Number, true);
SPFieldLink objSPFieldLink1 = new SPFieldLink(objSPWeb.Fields["Dynamic Number Column"]);
objSPContentType.FieldLinks.Add(objSPFieldLink1);
// A required String Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic String Column", SPFieldType.Text, true);
SPFieldLink objSPFieldLink2 = new SPFieldLink(objSPWeb.Fields["Dynamic String Column"]);
objSPContentType.FieldLinks.Add(objSPFieldLink2);
//A non-required Choice Field
// Create Site Column
objSPWeb.Fields.Add("Dynamic Choice Column", SPFieldType.Choice, false);
SPFieldChoice choicefield = (SPFieldChoice)objSPWeb.Fields["Dynamic Choice Column"];
// Add a group to the filed
choicefield.Group = "Dynamic Group";
// Add choices
choicefield.Choices.Add(string.Empty);
choicefield.Choices.Add("Yes");
choicefield.Choices.Add("No");
// Set the default choice
choicefield.DefaultValue = string.Empty;
choicefield.Update();
SPFieldLink objSPFieldLink3 = new SPFieldLink(choicefield);
objSPContentType.FieldLinks.Add(objSPFieldLink3);
objSPWeb.Fields.AddLookup("Dynamic Lookup Column", new Guid("GUIDofRemoteList"), false);
SPFieldLookup lookupfield = (SPFieldLookup)objSPWeb.Fields["Dynamic Lookup Column"];
// Set the remote field
lookupfield.LookupField = "RemoteFieldName";
lookupfield.Update();
SPFieldLink objSPFieldLink4 = new SPFieldLink(lookupfield);
objSPContentType.FieldLinks.Add(objSPFieldLink4);
objSPWeb.ContentTypes.Add(objSPContentType);
objSPContentType.Update();
}
}
}
}
}
The code snippet is the Console Application, and is ready to use.
Come from: http://www.directsharepoint.com/2011/04/dynamically-creating-sharepoint-content.html
相关文章推荐
- sharepoint ECMA using a custom contentType to creating a list in SubSite
- Creating and Using a New Content Type in SharePoint 2007(zhuan:http://www.sharepointproconnections.com/article/sharepoint-portal
- add a custom action to a SharePoint list actions menu for a specific list or content type
- how to set default content type in document set in Sharepoint
- Think in SharePoint 2010 OOTB Project: Site Columns & Site Content Type
- 学习:Creating a Custom Web Service for SharePoint(转)
- Think in SharePoint 2010 OOTB Project: Site Columns & Site Content Type
- SharePoint Development - Custom Content Type using Visual Studio 2010 based SharePoint 2010
- sharepoint content type publishing
- Sharepoint Custom Control Creating custom Multi Choice field type
- SharePoint State Machine Workflow +ASP.NET ASPX Form step by step(1)-创建Workflow Content Type
- Upload a file to Sharepoint 2007 using webservices with a specific content type
- Find SharePoint object type from URL
- 一步步学习SPD2010--第一章节--探索SPD2010(3)--理解SharePoint Designer 2010新功能
- 步步为营 SharePoint 开发学习笔记系列 九、SharePoint web service 开发(上)
- 转载Inside SharePoint Creating an External Storage Solution for SharePoint --Author Pav Cherny
- Creating Client Extranets with SharePoint 2003
- Error creating bean with name 'contentTypeAdminController': Injection of autowired dependencies fail
- Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统
- 使用SharePoint Web Serivce(4)-HyperLink Type Column 中令我发狂的空格