基本内容类型“事件"缺少VS 2010 [英] Base content type "Event" missing VS 2010

查看:41
本文介绍了基本内容类型“事件"缺少VS 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式基于事件内容类型创建自定义内容类型.列表中缺少事件父内容类型,无法从要继承的父内容类型中进行选择.我正在使用Visual Studio 2010.

I am trying to create custom content type based on Event content type programmatically. Event parent content type is missing from the list to choose from parent content type to inherit. I am using visual studio 2010. 

在此先感谢您的帮助!

推荐答案

您好,

您可以尝试使用Doug共享的以下代码,以编程方式创建内容类型并将其添加到列表中.

http://www.sharepointdoug.com/2012/12/programmatically-creating -content-type.html

//Create a new content type. Note that it inherits from item
	SPContentTypeId itemContentTypeId = new SPContentTypeId("0x01");
	SPContentType myContentType = Utility.CreateSiteContentType(web, "Content Type Name", itemContentTypeId, "Grouping Name for the Custom Content Type");

	//Write the changes to the database
	web.AllowUnsafeUpdates = true;
	myContentType.Update();
	web.AllowUnsafeUpdates = false;

	//Add a single choice field to the site as a site column
	SPField field = Utility.CreateSiteColumn(web, "My Choice Field", SPFieldType.Choice, "My Custom Fields");
	field.Title = "Type of Data";
	field.Description = "Audience : Choice field used to determine whether the requested video is for internal, external or both internal and external dissemination.";

	//Add the new field to the new content type
	Utility.AddFieldToContentType(web, myContentType.Id, field);

	//Write the changes to the database
	web.AllowUnsafeUpdates = true;
	myContentType.Update();
	web.AllowUnsafeUpdates = false;

	//Add a new list to the site
	Guid guid = web.Lists.Add("List Name", "List Description", SPListTemplateType.GenericList);

	//Write the changes to the database
	web.AllowUnsafeUpdates = true;
	web.Update();
	web.AllowUnsafeUpdates = false;

	//Add the new content type to the new list
	SPList myList = web.Lists.GetList(guid, false);
	myList.ContentTypesEnabled = true;
	myList.ContentTypes.Add(myContentType);
//Write the changes to the database
	web.AllowUnsafeUpdates = true;
	myList.Update();
	web.AllowUnsafeUpdates = false;


public static class Utility
{
	public static SPContentType CreateSiteContentType(SPWeb web, string contentTypeName,
		SPContentTypeId parentItemCTypeId, string group)
	{
		if (web.AvailableContentTypes[contentTypeName] == null)
		{
			SPContentType itemCType = web.AvailableContentTypes[parentItemCTypeId];
			SPContentType contentType = 
				new SPContentType(itemCType, web.ContentTypes, contentTypeName) {Group = @group};
			web.ContentTypes.Add(contentType);
			contentType.Update();
			return contentType;
		}
		return web.ContentTypes[contentTypeName];
	}

	public static SPField CreateSiteColumn(SPWeb web, string displayName,
		SPFieldType fieldType, string groupDescriptor)
	{
		if (!web.Fields.ContainsField(displayName))
		{
			string fieldName = web.Fields.Add(displayName, fieldType, false);
			SPField field = web.Fields.GetFieldByInternalName(fieldName);
			field.Group = groupDescriptor;
			field.Update();
			return field;
		}
		return web.Fields[displayName];
	}
		
	public static void AddFieldToContentType(SPWeb web,
		SPContentTypeId contentTypeId, SPField field)
	{
		SPContentType contentType = web.ContentTypes[contentTypeId];
		if (contentType == null) return;
		if (contentType.Fields.ContainsField(field.Title)) return;
		SPFieldLink fieldLink = new SPFieldLink(field);
		contentType.FieldLinks.Add(fieldLink);
		contentType.Update();
	}
}

OOB SharePoint内容类型供您参考.

如果您将来需要帮助,能否提供更多详细信息并共享示例代码.

最好的问候,

Lee


这篇关于基本内容类型“事件"缺少VS 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆