SharePoint 2013 Custom Content Type with Site Column custom validations
Hello,
Can somebody please suggest me how I can create custom content type with site columns with custom validation to site columns programmatically?
Thanks,
Praveen Kumar Padmakaran
Hi,
From your description, my understanding is that you want to create content type with site column with validation.
You could create a site column, and add some validation to the site column. After you could create a custom content type, please add the site column with validation to the content type. Please refer
to this code below:
static void Main(string[] args)
// replace your url
using (SPSite site = new SPSite("http://sp/sites/sp2013"))
using (SPWeb web = site.OpenWeb())
//define the type of the field
SPFieldType type = SPFieldType.Number;
// create a site column
SPField field = CreateSiteColumn(web, "newTest", type, "");
// add custom formula for the field
SPFieldNumber fieldNumber = web.Fields.GetField("newTest") as SPFieldNumber;
fieldNumber.ValidationFormula = "=[newTest]>5";
fieldNumber.ValidationMessage = ">5";
fieldNumber.Update();
SPContentTypeId parentItemCTypeId = web.ContentTypes[0].Id;
// create custom content type
SPContentType contentType = CreateSiteContentType(web, "newContent", parentItemCTypeId, "Custom Content Types");
// add the site column to the content type
AddFieldToContentType(web, contentType, field);
// add fiedl to contenttype
public static void AddFieldToContentType(SPWeb web, SPContentType contentType, SPField field)
if (contentType == null) return;
if (contentType.Fields.ContainsField(field.Title)) return;
SPFieldLink fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
contentType.Update();
// create a custom content type
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];
// create a site column
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];
You could refer to these articles:
C# code to create Site Column, Content Type, and add fields to Content Type
http://spshare.blogspot.jp/2013/10/c-code-to-create-site-column-content.html
How to do custom validation for site column in SharePoint
http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-do-custom-validation-for-site-column-in-sharepoint/
Best Regards,
Vincent Han
TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
[email protected].
Similar Messages
-
I have a team site collection and I want to add a new App of type Issue Tracking list. so I did the following:-
From the site collection I created a new App of type issue tracking.
Then from the site collection I created a new Content type named “CustomIssue” which has its parent as “Issue” content type.
I went to the Issue tracking list and I changed the default content type from Issue , to the new “CustomeIssue” content type.
I open the site collection using SP designer and I created a new Create form for my Issue tracking list based on the "CustomIssue" content type and I select to have the Create form as the default form when creating an item.
Everything till this point worked well. But when I open the “customIssue” content type , and I re-order the columns and I hide some columns, this was not reflected inside the custom Create form …
although when using the default content type and the default create form you can control the order of the fields and to specify if certain fields hold be hidden inside the Create form.. so can anyone advice on this please?Hi,
According to your post, my understanding is that you lost the ability to order and hide site columns if i use custom content type with a custom Create Form.
I try to reproduce the issue, the result is the same as yours.
As a workaround, if I modify the custom content type form the site setting, and then change the NewForm as the default form, it will change the column orders.
However, if I use the new created form as the default form, it will remain the original orders.
I recommend that you modify the custom content type form the site setting, and then reset the NewForm as the default form.
The result is as below:
Best Regards,
Linda Li
Linda Li
TechNet Community Support
ok thanks for the explanation ,, but what if i want to change the order and hidde some fields in the future ,, do i have to chnage the defualt create form again ... -
Site content types and site columns were absent
hi,
i have seved a sub site as a template which includes 20+ doc libs with 20+ site content ctypes which includes 15+ site columns and then i have used this savedsite template in one site collection and implemented my
custom web parts and upload document fun. successfully.
now i have created another new site collection and forgot to create site content types and site columns as we have seen the site columns in the saved site template already.
now i realized that those site content types and site columns were not there but the strange thing is that i am able to uplaod the documents into those document libraries which are not associated with site content types, without any error.
Can I create the same in my new site collection ? since i have already created many many sub sites based on the saved site templates tested the functionality ,i dont wanna delete the template recreate all the sites again in order
to save the time.
Now pls advice:
1) if site columns / site contents were not existing at the site collection level, will this affect my func. in future?
2) is there any automated way fo creating site columns, site content types, associateing these site content types with doc libs programmatically like power shell/ sp object model API.
3) would like to any programmatic way of creating hundreds of sub sites based on the saved site templates in myw eb application/ site collection.Hi,
We can create site columns, site content types or sub-sites using SharePoint Server Object Model.
The following articles for your reference:
SharePoint 2010: Create Site Columns and Content Types using C#.Net
http://social.technet.microsoft.com/wiki/contents/articles/20267.sharepoint-2010-create-site-columns-and-content-types-using-c-net.aspx
How to: Add a Content Type to a SharePoint List
http://msdn.microsoft.com/en-us/library/office/aa543576(v=office.14).aspx
Create Sites Using Custom Site Templates in SharePoint 2010
http://www.c-sharpcorner.com/UploadFile/63e78b/create-sites-using-custom-site-templates-in-sharepoint-2010/
How to create sub site with custom site template through PowerShell
http://fangdahai.blogspot.com/2012/08/how-to-create-sub-site-with-custom-site.html
Best regards
Dennis Guo
TechNet Community Support -
How to Create Custom Content Type with 100 site columns ?
Hi EveryOne,
i have one requirement to create custom conten types in sharepoint 2013 as follows.
1. Create a content type with 100 site columns ( in this 100 site columns includes 10 mms feilds and 10 lookup fields).
2.when we deploy the content type in other server if already the same content type existing in the server it should be upgrade the existing content type.
Please advise how do develop the solution ( using power shell script or visual studio with event receiver or xml file)
Regards,
SrinivasTry below:
http://www.mindfiresolutions.com/Add-Columns--Fields-Programmatically-to-a-SharePoint-List-282.php
using(SPSite
oSPsite = new SPSite("http://Web-URL"))
oSPsite.AllowUnsafeUpdates =
true;
using (SPWeb oSPWeb = oSPsite.OpenWeb())
oSPWeb.AllowUnsafeUpdates =
true;
/* get the SPList object by list name*/
SPList lst = oSPWeb.Lists["EmpList"];
/* create a Numeric field for EmpID */
SPFieldNumber fldEmpID = (SPFieldNumber)lst.Fields.CreateNewField(
SPFieldType.Number.ToString(),
"EmpID");
fldEmpID.Required =
true;
fldEmpID.DisplayFormat =
SPNumberFormatTypes.NoDecimal;
/* create a Text field for Name */
SPFieldText fldName = (SPFieldText)lst.Fields.CreateNewField(
SPFieldType.Text.ToString(),
"Name");
fldName.Required =
true;
fldName.MaxLength = 50;
/* create a Date field for Dob*/
SPFieldDateTime fldDob = (SPFieldDateTime)lst.Fields.CreateNewField(
SPFieldType.DateTime.ToString(),
"Dob");
fldDob.DisplayFormat =
SPDateTimeFieldFormatType.DateOnly;
/* create a Currency field for Salary */
SPFieldCurrency fldSal = (SPFieldCurrency)lst.Fields.CreateNewField(
SPFieldType.Currency.ToString(),
"Salary");
fldSal.Currency =
SPCurrencyFieldFormats.UnitedStates;
fldSal.DisplayFormat =
SPNumberFormatTypes.TwoDecimals;
/* add the new fields to the list */
lst.Fields.Add(fldEmpID);
lst.Fields.Add(fldName);
lst.Fields.Add(fldDob);
lst.Fields.Add(fldSal);
/* finally update list */
lst.Update();
oSPWeb.AllowUnsafeUpdates =
false;
oSPsite.AllowUnsafeUpdates =
false;
If this helped you resolve your issue, please mark it Answered -
Content types, Required site columns, How are they supposed to work?
Hi,
I have 2 issues:
I created a Folder level content type with many Required site columns. I was hoping that when I create a new folder the new content type screen will pop up and I must enter all the required properties (metadata) for this new folder.
Issue 1) But it didn't work that way. It just required me to enter the name of the folder and the folder was created.
Issue 2) I then went to Edit Properties to enter enter all the required metadata. I would like to have my newly created content type to be the default. So that when I open up the Edit Properties the popup screen would defalt to my new
content type. But it defaults to "Folder" Content type. even though in liberary setting, My new content type was the only one check "visible" and set as default.
Can someone please help? Am I missing something?
Thanks!I don't think you can do what you want to do using only out of the box features. A few notes:
Users will need to click the New Document dropdown in the ribbon to select your new folder content type. (I.e. Don't click New Folder)
The "default" option in the content type list is to pick the default content type to be selected when you click the New Document button. (I.e. it won't impact the New Folder ribbon button)
The ribbon button for New Folder is hard coded to use the built in folder feature.
You may want to look into the 2010 Document Set feature to create folders with metadata. It will do what your custom content type does and a lot more.
Possible solutions:
Create JavaScript hack that changes the New Folder link in the ribbon to go to the New Document link for your custom content type.
Create a Visual Studio Feature to hide the New Folder button and add a new New Folder button that points to the New Document link for your custom content type.
Leave the existing New Folder button there and create Feature to add a new custom button for your content type.
Mike Smith TechTrainingNotes.blogspot.com
my SP customization book -
Required field in custom content type not required in custom list instance
I have an issue with a custom SharePoint 2013 solution. Among other components, it consists of:
a feature with several custom fields with different types, some of which are taxonomy fields,
a feature with three custom content types using different sets of the custom fields with partially different configuration, e.g. whether they are required or not,
a feature with a custom document library template and instance using two of the custom content types and a default picture library that is programmatically customized when the feature is activated, e.g. it is assigned the third custom content type.
When deploying the solution and activating the feature, everything is set up correctly except a single taxonomy field for one of the doc lib's content types. It is defined to be required in both content types but indeed it does not show up as required for
the default content type whereas everything works fine for the other. And it does not matter which of the two custom content types is defined first (= default) in the list template's schema.xml, the issue always occurs for the same taxonomy field in the doc
lib's default content type. When I use the built-in Document content type as default, the field is required for both custom content types. However, using the Document content type is not an option.
If you think this behavior is not strange enough: When the list content type's field is set required through the SharePoint UI, it becomes optional again when the list column is updated, e.g. its display name is changed.
Any ideas?Hi,
Thank you for your question.
We are currently looking into this issue and will give you an update as soon as possible.
Thank you for your understanding and support.
Linda Li
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
[email protected]
Linda Li
TechNet Community Support -
Can I add Custom Content Type created programmatically to Custom List Definition?
I have created Custom content type programmatically in the Feature Receiver. Followed by this have another feature which creates List Template. I want to add the custom content type to the list template.
Using VS 2012, I added the custom content type to the list definition. The Schema.xml for the list definition looks as below:
<ContentTypes>
<ContentTypeRef ID="0x010100D7D9F4B1F4A9684BB44389571024B2EC00C393BB21B8AD7B41B62A87DF0501504D" />
</ContentTypes>
ID was automatically added by the VS tool.
List Template is created, the new content Type is also added, but the Name is displayed as "Document" instead of the custom content type name. So I see two CTs with the name "Document".
How can I achieve this? Any help is appreciated.Hi,
Please add EnableContentTypes="TRUE" in the <List > tag.
The following materials would be helpful:
How to add custom content type to a custom list
http://innersharepoint.blogspot.de/2009/10/how-to-add-custom-content-type-to.html
SharePoint Custom List Definition with Content Type
https://achrafsp.wordpress.com/2013/03/31/sharepoint-list-definition-with-content-type/
Create a Custom SharePoint List Definition
http://www.mssharepointtips.com/tip.asp?id=1188
Best Regards
Dennis Guo
TechNet Community Support -
SharePoint 2013 Folder Content Types
Hi,
In SP2013 I have library with a default content type (IT Library Content Type) that I want to change when a file is placed in a particular folder in this library.
The default content type order for this folder has been changed to a different content type 'Issues' and should be the only content type shown when the edit page is presented. However, when a file is added to this folder, a Content Type selection
listbox is presented showing 'It Library Content Type' as the default selection with the 'Issues' content type available for selection.
Desired behavior is to have 'Issues' content type columns presented, with no content type selections seen, when the Edit Page is shown.
ThanksHi,
As you said about Folder content type in SharePoint, I supposed that you were talking about Document Set content type.
If I misunderstood, please feel free to correct me.
If that is the case, the issue is by design that the default content type will be available when uploading or editing documents inside the document set even though the content type is not added in the allowed content types in Document Set settings. And the
default content type of the library will be set the default selection when editing or uploading documents inside the document set.
As a workaround, I recommend to set the allowed content type as the default content type in the document library.
Best regards.
Thanks
Victoria Xia
TechNet Community Support -
Hi all,
I'm using SharePoint Online, but I'm seeing the same behavior in an on-prem 2013 instance as well. My situation is this:
- I've created a document library
- I've created a custom content type and attached a custom document template to it
- I've assigned the custom content type to the document library, and disabled the default "document" option
What I'm expecting to see is that when I browse to the document library and click "new document", that either a) a picklist appears allowing me to specify the document template I want (using the custom template I specified) or b) open the custom
template itself. That doesn't happen - instead, when I click new document I'm prompted to upload a file, which seems to contradict the whole point of using a custom content type/custom document template combo.
Am I missing something? The custom template isn't in the Forms library, which seems to be a problem if I wanted to use the custom document template instead of the default.
Ideally I'd like a menu like the one shown here:
http://social.msdn.microsoft.com/Forums/en-US/59ce3bd8-bf7f-4872-ae76-60d8c81dfc32/display-content-types-on-new-document-button-in-document-libraries?forum=sharepointgeneral, except with me being able to control the list of items that is shown.
Any ideas? Thanks!Hi Brain,
What you have done is by design behavior.
If you want to show the Office document templates list (e.g. below image from your above referenced link) to select when click "+new document" link, this will need to install Office Web App 2013 which provides this feature,
you can new document and see it is using WopiFrame.aspx page, please see more from below article about how to configure OWA 2013 for SharePoint 2013 on-premise.
http://technet.microsoft.com/en-us/library/ff431687(v=office.15).aspx
Thanks,
Daniel Yang
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected]
Daniel Yang
TechNet Community Support -
Displaying Custom Content Types
Short Version
I have created custom content types that has a few properties that are complex. One is an image type and another is a collection of another custom content type. I am unable to display or access these items.
Long Version
I have a custom content type with the following properties
It is called PageThreeColumn
PageTitle : String
LeftNav : String
RightNav : String
HeroImage : image (complext type that came with weblogic)
ContentCollection: Collection of type ContentPlaceholder
The other custom type ContentPlaceHolder
Type : String
HtmlContent : Binary
I created a content presenter view (wlp-templete-config.xml) and I am able to get a simple string property to be displayed.
So I can do the following
<cm:getProperty name="LeftNav" node="${node}"/>
And I get the expected String back.
What I am unable to do is get to anything that is more complicated then the simple datatypes. For instance the Image data type.
I have tried node.HeroImage but it does not display correctly.
I appreciate any help you can give and would definately like a link to an example of displaying content that is not as trivial as most of the examples.I have tried the following code variations
<div>
${node.properties[0]}
<!--//Shows string as expected-->
${node.properties[1]}
<!--//Shows string as expected-->
${node.properties[2]}
<!--//Shows string as expected-->
${node.properties[3]}
<!--//Shows the following
HeroImage: [Lcom.bea.content.Property;@48958a1 -->
${node.properties[4]}
<!--//Shows the following
ContentCollection: [Lcom.bea.content.Property;@48958a5 ,
[Lcom.bea.content.Property;@48958a8 ,
[Lcom.bea.content.Property;@48958ab -->
<cm:getProperty node="${node.HeroImage}" name="heroimage" />
<img src="${templates:getImageSource(pageContext.request, heroimage)}"/>
</div>
Variation 2
<img src="${templates:getImageSource(pageContext.request, node.HeroImage)}"/> //says that it does not recognize node.HeroImage
Variation 3
${node.HeroImage} //says that it does not recognise HeroImage
All dont work.
Should I be adding or doing some type of customization to the template jsp to handle the complex data type?
Is there an example or tutorial that uses a complex content type and the content presenter to show content?
Or maybe an example of more complicated uses of the JSP files in the:
/portlets/wlp/contentpresenter/templates/wlp-default/cm
/portlets/wlp/contentpresenter/templates/wlp-default/outer
All of the examples I found were ver trivial. Basicly showing a very simple example but nothing using a content type that is more complicated in the non-primary properties then strings.
Edited by: danscan1712 on Mar 11, 2009 8:01 AM -
Hi,
I have a requirement to read the External Content Type and storing the Data in a Session Variable. The Reading of the content from External Content Type with out creating a External List.
Please help I am trying to find the solution, but unable to do that.
Thanks,
PradeepHi,
Firstly an external content type designed to work with SharePoint list and there is no way to read apart from this.
If you are looking the solution out of the " Business Connectivity" then find that data source has been exposed through WCF and Web Service ?
So you can use REST API and CSOM to consume those data in SharePoint.
You can also leverage the ADO.NET option if the datasources based on MS technologies.
Murugesa Pandian| MCPD | MCTS |SharePoint 2010 -
Dear SharePoint Developers,
Please help.
I need to know How to set the column order of a sealed column in a custom Content Type for the new item form NewDocSet.aspx?
I think this is a "sealed column", whatever that is, which is shown in SPD 2013 as a column of content type "document, folder, MyCustomContentType".
I know when I set the column order in my custom Content Type settings page, it is correct.
But, when I load the NewDocSet.aspx page, the column order that I set in the settings page is NOT used for this "sealed column" which is bad.
Can you help?
Please advise.
Thanks.
Mark Kamoski
-- Mark KamoskiHi,
According to your post, my understanding is that you want to set the column order of a sealed column in a custom Content Type for the new item form NewDocSet.aspx.
Per my knowledge, if you have Content Type management enabled for the list or library (if you see a list of content type with the option to add more), the display order of columns is set for each content type.
Drill down into one of them and you'll see the option under the list of columns for that content type.
To apply the column order in the NewDocSet.aspx page, you need to:
Select Site Settings, under Site Collection Administration, click Content type publishing. In the Refresh All Published
Content Types section, choose Refresh all published content types on next
update.
Run two timer jobs(Content Type Hub, Content Type Subscriber) in central admin(Central Administration--> Monitoring--> Review timer jobs).
More information:
http://sharepoint.stackexchange.com/questions/95028/content-types-not-refreshing-on-sp-online
Best Regards,
Linda Li
Linda Li
TechNet Community Support -
Created column not showing in the Document Library View for a Custom Content Type based on Document
We have a custom content type based of Document Content Type. The OOTB "Created" column does not show up in the view as well as in the Site Settings -> Columns.
But it shows up in the Display Form and Edit Form of the Item at the bottom "Created at " by " " and "Modified by" at by ""
Would anyone know how to make this column appear as part of the view ?
thanks,
HarshHi,
They should by default be possible to add to a view. The only reason they would disappear from the UI if someone has changed the attribute of the Field to hidden = true. You can verify that with PowerShell and if so you can use PowerShell to revert the setting
to false.
$w = Get-SPWeb http://dev13$f = $w.Fields.GetFieldByInternalName("Created")$f.Hidden$f.Hidden = $false$f.Update() -
I have two different Site Asset libraries. The Site Asset Picture library has items of
content type picture. The Site Asset Image Library has items of
content type image.
1) I do not quite understand the different between a picture content type versus an image content type.
2) the thumbnail and preview columns do not display the image in the Site Asset
Image Library. (the image does display in the thumbnail column of the Site Asset Picture library.) I expected the image to display in the Site Asset Image Library the same as it does in the Site Asset Picture library.
Why doesn't it?
Betty
Betty StolwykI have subsequently found out that our "Site Asset Images" library is a Document Library to which someone must have replaced the Document content type with an Image content type. I also found out that picture previews/thumbnails cannot be displayed
in Document libraries. So the problem was the library type, not the content type.
So I simply created a new Picture Library (whose content type = picture) and copied the files into that and the thumbnails displayed just fine!
I bet what led to this is the default "Site Assets" library that is created for a new Team site. It is just a document library but sounds very similar to the "Assets" library. I can imagine someone thinking the "Site Assets" library was a standard
container for images, and simply changed the name to better identify the content!
As far as 'image' vs 'picture' content types, I noticed that a new Picture Library contains a content type of 'picture', but an Asset Library contains a content type of 'image' (as well as audio and video). Although the 'image' content type has a few
more fields than the 'picture' content type (picture content type does not have thumbnail), the actual Picture Library contains pretty much all the same fields, including the thumbnail. So for all practical purposes, either library type can be used for
storage of pictures. I am guessing that the Pictures Library which uses the picture content type is for backwards compatibility purposes, and that the 'image' content type is newer, being derived from the new "Rich Media Asset" content type and used
in the new Asset Library which can hold audio and video in addition to pictures.
Microsoft Help also mentioned that a Picture Library has a slide view which an Asset library does not. So my take on that is that if you want the potential of seeing your pictures as a slide show, put them in a Picture library which is still using
the 'picture' content type of previous SharePoint versions. If you want to keep pictures organized with audio/video files, then put them into an Asset library which uses the 'image' content type derived from the new Rich Media Asset content type.
Betty Stolwyk -
i am getting "could not save list changes to the server" while creating a new form in SharePoint Designer.
Actually the list was created with a custom content type. When i try to create a new form from the custom content type, it fails.
However, when i try to create a form using item content type, it creates a form successfully.
Could you please let me know hte workaroud for this.
TnxHello,
This link may be help you to create custom form using content types:
http://blog.splibrarian.com/2011/03/21/using-content-types-to-modify-the-newform-aspx-and-editform-aspx-pages/
Hemendra:Yesterday is just a memory,Tomorrow we may never see<br/> Please remember to mark the replies as answers if they help and unmark them if they provide no help <br/>(On vacation from 16th Oct to 28th Oct 2014)
Maybe you are looking for
-
Query with links to 'services for object'
Hi everybody I need your help We have pictures (jpg, pdf, tif) linked to materials in material master (in transaction mm02 services for object). Sometthing like this - but form materials: http://www.le.ac.uk/mis/docs/sapdocs/ug/UG5-1%20Services.doc
-
Windows for Mac....Highly dissapointed with LIon.
I use Windows for Mac for ALL of my studies and now I can not open any of my .doc files...any ideas?
-
Developer acecss key requested on executing proxy
Hi Guys, We just upgraeded our SAP PI system from PI 7.11 to PI 7.3. After upgrade we have regenerated the proxy for one of the existing custome development (File to Proxy sceario). Now we are facing 2 wierd issues: 1. On trying to execute the proxy
-
Mac Book Air 13" the right device for occasionally editing photos?
Hello! I am owner of an iMac 24" and know I want to replace my three years old thinkpad. Mainly, I am using my iMac for editing photos. With the MacBook Air I also want to be flexible to edit my photos also during a holiday or in the train etc. Do yo
-
Why can't I reply to emails on my iPhone 5 - this is a recent problem, I have been able to reply and delete email messages previously.