Parent Child Content Type using SharePoint Object Model

No.of Views1752
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
Parent Child Content Type using SharePoint Object Model
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 Objective
In this article, I am going to show how programmatically Content type could be fetched. How we could manage parent child relationship of the Content types.

Explanation

Step 1
I have created two Site Content types.
Note: To see how to create Content type sees my other articles on this site.

TestParent is parent content type and TestChild is chid content type.

TestParent content type

1. Parent of this content type is Folder
2. I added one column called OrderNumber as Number

Image loading....


TestChild Content Type

1. Parent of this Content Type is Item
2. I added two columns OrderItem and SKU

image loading...


Step 2
I associated these two content types to a SharePoint List called Test.
Note: To see how to attach Site Content type to SharePoint List see my other articles on this site.


image loading...


Step 3

I am putting some data in the list. The way to put data in list is as follows
1. First take Test Parent. Then fill all the column values for this.
2. Inside Test Parent create a Test Child and fill the column values.
3. Create more than one Test Child inside one Test Parent.

List is as below

image loading...


Inside First Order

image loading....

Inside Second Order

image loading....

Code to Fetch Content Type
I have created a console application and inside that I am going to display all the list items.


{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

SPSite spsite = new SPSite("http://adfsaccount:2222/");
SPWeb mysite = spsite.OpenWeb();




SPList parentList = mysite.Lists["Test"];
foreach (SPListItem i in parentList.Folders)
{

Console.WriteLine("Order : {0}", i["Name"]);
Console.WriteLine("Order Number : {0}", i["OrderNumber"]);
SPQuery orderitemsquery = new SPQuery();
orderitemsquery.Folder = i.Folder;
SPListItemCollection orderItems = parentList.GetItems(orderitemsquery);
foreach (SPListItem p in orderItems)
{
Console.WriteLine("\t Line Item from Child {0} : {1} {2}", p["OrderItem"], p["SKU"], p["Title"]);
}


}

Console.Read();
}
}
}

{/codecitation}

Explanation of code

1. SPSite is returning the site collection.
2. SPWeb is returning top level site.
3. SPList is returning the particular list. Test is name of the list here.
4. I am fetching all the folders in the list using Foreach statement.
5. I am retrieving the entire list inside a folder and printing that.

Output

image loading....

Conclusion

In this article, I have shown how to retrieve parent child related content types using SharePoint object model. Thanks for reading.


Happy Coding


About the Author


Dhananjay Kumar
Description :I done my engineering from Anand Engineering college Agra in 2007. I am MCTS WCF, MCTS MOSS Development, I am MCTS Web Development . I am native of Jamshedpur. Currently Please feel free to contact me regarding any clarification of my article at Dhananjay.25july@gmail.com

Occupation : Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dhananjay25

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top