Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9
This is the forum to discuss the Wrox book Professional SharePoint 2007 Development by John Holliday, John Alexander, Jeff Julian, Eli Robillard, Brendon Schwartz, Matt Ranlett, J. Dan Attis, Adam Buenz, Tom Rizzo; ISBN: 9780470117569
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 16th, 2008, 07:35 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter-11 Content Type Wrapper

I'm hoping there's someone out there with much more experience with this stuff than I . My conundrum is, I'm trying to create a content type (just so I can simply see it in Site Settings/Content Types). Using the book code, I start out with the content type wrapper file GenericContentTypeWrapperClass.cs as follows:

using System;
using Microsoft.SharePoint;
namespace ProSharePoint2007
{
    /// <summary>
    /// A utility class for manipulating SharePoint content types.
    /// </summary>
    public class ContentType
    {
        SPContentType m_contentType = null;
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ContentType()
        {
        }
        /// <summary>
        /// Creates a wrapper for an existing content type instance.
        /// </summary>
        /// <param name="contentType"></param>
        public ContentType(SPContentType contentType)
        {
            m_contentType = contentType;
        }
        /// <summary>
        /// Adds a content type to a SharePoint list.
        /// </summary>
        public static void AddToList(SPList list, SPContentType contentType)
        {
            list.ContentTypesEnabled = true;
            list.ContentTypes.Add(contentType);
            list.Update();
        }
        /// <summary>
        /// Removes a content type from a SharePoint list.
        /// </summary>
        public static void RemoveFromList(SPList list, string contentTypeName)
        {
            foreach (SPContentType type in list.ContentTypes)
            {
                if (type.Name == contentTypeName)
                {
                    list.ContentTypes.Delete(type.Id);
                    list.Update();
                    break;
                }
            }
        }
        /// <summary>
        /// Loads a pre-existing content type.
        /// </summary>
        public virtual SPContentType Create(SPWeb web, string typeName)
        {
            try
            {
                m_contentType = web.AvailableContentTypes[typeName];
            }
            catch
            {
            }
            return m_contentType;
        }
        /// <summary>
        /// Creates a new content type.
        /// </summary>
        public virtual SPContentType Create(SPWeb web, string typeName,
                            string baseTypeName,
                            string description)
        {
            try
            {
                SPContentType baseType = (baseTypeName == null
                    || baseTypeName.Length == 0) ?
                    web.AvailableContentTypes[SPContentTypeId.Empty] :
                    web.AvailableContentTypes[baseTypeName];
                m_contentType = new SPContentType(
                    baseType, web.ContentTypes, typeName);
                m_contentType.Description = description;
                web.ContentTypes.Add(m_contentType);
            }
            catch
            {
            }
            return m_contentType;
        }
        /// <summary>
        /// Conversion operator to access the underlying SPContentType instance.
        /// </summary>
        public static implicit operator SPContentType(ContentType t)
        {
            return t.m_contentType;
        }

        #region Field Methods
        /// <summary>
        /// Adds a new field having a specified name and type.
        /// </summary>
        public SPField AddField(string fieldDisplayName,
                        SPFieldType fieldType, bool bRequired)
        {
            SPField field = null;
            try
            {
                // get the parent web
                using (SPWeb web = m_contentType.ParentWeb)
                {
                    // create the field within the target web
                    string fieldName =
                        web.Fields.Add(fieldDisplayName,
                                fieldType, bRequired);
                    field = web.Fields[fieldName];
                    // add a field link to the content type
                    m_contentType.FieldLinks.Add(
                        new SPFieldLink(field));
                    m_contentType.Update(false);
                }
            }
            catch
            {
            }
            return field;
        }
        /// <summary>
        /// Adds a new field based on an existing field in the parent web.
        /// </summary>
        public SPField AddField(string fieldName)
        {
            using (SPWeb web = m_contentType.ParentWeb)
            {
                SPField field = web.AvailableFields[fieldName];
                try
                {
                    if (field != null)
                    {
                        m_contentType.FieldLinks.Add(
                            new SPFieldLink(field));
                        m_contentType.Update(false);
                    }
                }
                catch
                {
                }
                return field;
            }
        #endregion
        }
    }
}


Next, I'm creating the ProjectProposal Content Type with the following ProjectProposalCT.cs file also using the book code:

namespace ProSharePoint2007
{
    /// <summary>
    /// A helper class that encapsulates the ProjectProposal content type.
    /// </summary>
    class ProjectProposalType : ContentType
    {
        /// <summary>
        /// Creates the type using the XML content type definition.
        /// </summary>
        public SPContentType Create(SPWeb web)
        {
            return this.Create(web, "Project Proposal");
        }
        /// <summary>
        /// Creates the type using the SharePoint object model.
        /// </summary>
        public override SPContentType Create(SPWeb web, string typeName,
                            string baseTypeName,
                            string description)
        {
            // Call the base method to create the new type.
            SPContentType tProposal = base.Create(web, typeName,
                baseTypeName, description);

            // Create the fields programmatically.
            if (tProposal != null)
            {
                // just use built-in fields for now
                AddField("Author");
                AddField("Subject");
                AddField("StartDate");
                AddField("EndDate");
                AddField("Status");
                AddField("Comments");
                AddField("Keywords");
                // custom fields
                //AddField(Strings.Field_ProposalType);
                //AddField(Strings._Field_EstimatedCost);
                //AddField(Strings._Field_BidAmount);
                //AddField(Strings._Field_EstimatedHours);
                //AddField(Strings._Field_HourlyRate);
            }
            return tProposal;
        }
    }
}

Now I build successfully, and move the resulting dll into the GAC and then iisreset.

The content type does not show up in the Content Types list. Isn't it supposed to simply show up there, since I'm not using the "featureactivated" event in a feature receiver, or do I have to take another step? The book code shows how to use an event receiver to use this content type and build a document library off it.

I just want it firstly to show up in the content Type list!!

Thanks
Help

  #2 (permalink)  
Old September 4th, 2009, 07:56 AM
Wrox Author
 
Join Date: Jun 2007
Location: Fernandina Beach, FL, USA.
Posts: 10
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to jholliday
Default

Once you have your dll in the GAC, you have to call one of the factory methods in order to create the content type. Typically, you would do this from within a feature receiver or some other code, as follows:

SPContentType newType = ProjectProposal.Create(web);

At this point, the new content type should appear in the gallery.
__________________
John F. Holliday, Author, MOSS MVP
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SOAP Content-Type error babchai .NET Web Services 0 May 11th, 2008 11:24 PM
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 05:11 AM
Content-type = text/html tarran C# 3 December 5th, 2004 07:36 AM



All times are GMT -4. The time now is 04:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.