.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Parent Child Content Type using SharePoint Object Model

Posted By:Dhananjay Kumar       Posted Date: February 01, 2010    Points: 25    Category: SharePoint    URL: http://www.dotnetspark.com  

Parent Child Content Type using SharePoint Object Model. 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.
 

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 
1.gif

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

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.

3.gif 

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

4.gif

Inside First Order

5.gif

Inside Second Order

6.gif

Code to Fetch Content Type

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

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();

        }

    }
}


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

7.gif

Conclusion

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

Happy Coding

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend