IT 관련 이야기/SharePoint

SharePoint 2007 Features – Creating Site Columns and Content types

종소리도깨비 2008. 6. 28. 15:05
반응형

MOSS 를 사용하다가 보면 Custom Column 이나 Content type을 등록해서 사용해야 할 때가 많다.
아래사이트에 설명된 부분을 참조하시라..

원문 : http://sharethispoint.com/archive/2006/07/17/11.aspx
 

SharePoint 2007 Features – Creating Site Columns and
Content types

So if you’ve been playing with SharePoint 2007 I’m sure you have heard of features. This post is going to attempt to provide a practical example of how to create 2 features that work together to accomplish a useful task.  One feature will create a few site columns and the other will create a content type that makes use of the sites columns created in the first feature.

 

Heres the scenario: We want to create a content type that defines columns for storing information about motor vehicles. For the purpose of this example this content type will be pretty basic, containing only three fields…..

  • Make
  • Model
  • Year

 Since we do not want to re-create this content type manually on every site collection that will use it we are going to create it as a feature. One caveat about content types is that all the columns defined with in the content type must also be site columns. So for that reason we are also going to create a feature that installs the three columns for this content type as site columns.  You could probably do both of these actions in one feature but I wasn’t able to get it to work.

 

Before we get into the details go ahead and create a folder some where on your local machine to put the files we are going to create into. We will refer to this as your working folder. We will eventually copy these files over to the server when we are done.

 

Lets start with the site columns first. There isn’t an example in the beta SDK for creating features that install site columns (That I could find anyway).  So the next best place to look is in the features directory of SharePoint. The path for this dir is typically…

 

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

 

I am more of a hands on kind of guy so having real examples always works good for me. Since most of the default stuff that is in a SharePoint site is created with features this is a great place to find examples of everything you can do with features.  The default SharePoint site columns are located in this file…

 

12\TEMPLATE\FEATURES\fields\fieldswss.xml

 

So by looking at the schema used in that file we can create the xml for our site columns...

Note: Never add stuff to the existing SharePoint features!! Always create your own!

 

<?xml version="1.0" encoding="utf-8" ?>

<!-- _lcid="1033" _version="12.0.4017" _dal="1" -->

<!-- _LocalBinding -->

<!--

-->

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<Field ID="{CC5A31EF-CCD5-4ed4-A691-2D343B320EA2}"

        Name="Make"

        SourceID="http://schemas.microsoft.com/sharepoint/v3"

        StaticName="Make"

        Group="Vehicle Data"

        Type="Text"

        DisplayName="Make" />

<Field ID="{B1105775-D16E-42ea-ABA1-1C38DD453823}"

        Name="Year"

        SourceID="http://schemas.microsoft.com/sharepoint/v3"

        StaticName="Year"

        Group="Vehicle Data"

        Type="Text"

        DisplayName="Year" />

<Field ID="{6CC4C31C-AA19-43de-8810-A11E1D20ADD5}"

        Name="Model"

        SourceID="http://schemas.microsoft.com/sharepoint/v3"

        StaticName="Model"

        Group="Vehicle Data"

        Type="Text"

        DisplayName="Model" />

</Elements>

 

This schema is pretty straight forward and simple. The Group attribute defines what group the site columns will be placed. If you enter the name of a group that doesn’t exist SharePoint will automatically create it. The Type attribute defines the data type for the field. If you want to know how to create different data types uses the fieldswss.xml as a reference. The only thing that might throw you for a loop here is the ID attribute of each field element. These are just a 32 character GUID. When you create your own fields, features, and other stuff in SharePoint you will need to generate these. The easiest way to create GUIDs is with the guidgen util that comes with Visual Studio. The path to that utility is…

 

"C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\guidgen.exe"

 

Just run the util and select registry format and you are good to go.

You will also notice that there is a feature.xml file in the same folder as fieldswss.xml. All features must have a feature.xml file. Feature.xml is very simple file that basically tells SharePoint where to find the rest of the code/xml for the feature and defines any dependencies. We will talk about these more later.

 

So now that we have our xml for the site columns go ahead and create a folder in your working dir called fieldsVehicle. In the fieldsVehicle folder create an xml file named fieldsVehicle.xml and copy and past the site column xml into it.

 

To finish this feature we need to create a feature.xml file in the fieldsVehicle folder and copy and paste the following text into it…

 

<?xml version="1.0" encoding="utf-8" ?>

<!-- _lcid="1033" _version="12.0.4017" _dal="1" -->

<!-- _LocalBinding -->

<Feature  Id="5C784DCD-0109-40c5-A647-B0972E10689A"

          Title="Vehicle Data Site Columns"

          Description="Installs columns designed to manage information about vehicles."

          Version="12.0.0.0"

          Scope="Site"

          DefaultResourceFile="core"

          xmlns="http://schemas.microsoft.com/sharepoint/">

    <ElementManifests>

        <ElementManifest Location="fieldsVehicle.xml" />

    </ElementManifests>

</Feature>

 

To create this file I just copied the existing one in the 12\TEMPLATE\FEATURES\fields\ folder and modified the values. One important thing to notice is that the ID attribute of the feature element does not have the “{}” characters at the beginning and end of the GUID. Some ID fields in SharePoint are formatted slightly different for some reason so you have to watch out for that.  But you can still generate these GUIDs the same way with the guidgen util. The scope attribute is set to Site. That means this feature will be installed/activated on the entire site collection. If you want to activate/deactivate your feature on a individual site level set the scope to
“Web”. When modifying this file also make sure that the elementmanifest location is set to the correct file you have created for your feature. In this case our fieldsVehicle.xml file.

 

Our site columns feature is now complete!! Yay! Now we can go ahead and start work on our content type feature. Since there doesn’t appear to be any examples in the SDK for creating features that install content types we are again going to refer to the default features that came w/ SharePoint. The default content types that SharePoint uses are in this file….

 

12\TEMPLATE\FEATURES\ctypes\ctypeswss.xml

 

Microsoft does have some good documentation (http://msdn2.microsoft.com/en-us/library/ms463449.aspx)  on the general xml schema we are going to be using to define the content type. So now that we have some examples of existing content types and some documentation on the schema we can create our own…

 

<?xml version="1.0" encoding="utf-8" ?>

<!-- _lcid="1033" _version="12.0.4017" _dal="1" -->

<!-- _LocalBinding -->

<!--

-->

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

    <ContentType ID="0x0100E71A2716C18B4e96A9B0461156806FFA"

        Name="Vehicle Data"

        Group="$Resources:List_Content_Types"

        Description="Designed to facilitate the storage of vehicle information."

        Version="0">

        <FieldRefs>

            <FieldRef ID="{CC5A31EF-CCD5-4ed4-A691-2D343B320EA2}" Name="Make" />

            <FieldRef ID="{B1105775-D16E-42ea-ABA1-1C38DD453823}" Name="Year" />

            <FieldRef ID="{6CC4C31C-AA19-43de-8810-A11E1D20ADD5}" Name="Model" />

        </FieldRefs>

    </ContentType>

</Elements>

 

One important thing to pay attention to here is the ID attribute of the content element. These are not very well documented in the schema. The ID here is a combination of a

value that indicates what other content types it inherits from and a GUID. The “0x0100” portion of the ID tells SharePoint that this content type inherits the item content type. The remainder of the ID is just a standard 32 character GUID with all the dashes “-“ and curly brackets “{}” removed. 

 

Just as a note on content type IDs: If you want to make your content type inheritable then you will need to leave the GUID out of the ID. The Document and the XML Document content types are good examples how to do this. Just make sure that your not trying to use an ID that SharePoint is already using.

 

The FieldRef elements in this schema simply references the site columns you added in the previous feature. The ID and name attributes should match the values of the site column you are referencing.

 

Now that we have our xml for the content type go ahead and create a folder in your working folder called ctypesVehicle. In the ctypesVehicle folder create an xml file named ctypesVehicle.xml and copy and past the content type xml into it.

 

As we did with the site columns we need to create a feature.xml file in the ctypesVehicle folder and copy and past the following xml into it…

 

<?xml version="1.0" encoding="utf-8" ?>

<!-- _lcid="1033" _version="12.0.4017" _dal="1" -->

<!-- _LocalBinding -->

<Feature  Id="6D145000-D35F-43af-83F3-797B283E3F2E"

          Title="Vehicle Data Content Types"

          Description="Installs content types designed to manage information about vehicles."

          Version="12.0.0.0"

          Scope="Site"

          DefaultResourceFile="core"

          xmlns="http://schemas.microsoft.com/sharepoint/">

    <ElementManifests>

        <ElementManifest Location="ctypesVehicle.xml" />

    </ElementManifests>

 <ActivationDependencies>

            <!-- Installz the site columns that hold vehicle data -->

            <ActivationDependency FeatureId="5C784DCD-0109-40c5-A647-B0972E10689A"/>

    </ActivationDependencies>

</Feature>

 

Notice that in this xml we are referencing the vehicle site columns feature as an activation dependency. This will ensure that the correct site columns are present before the vehicles content type is installed.

 

Now both of our features are complete! Copy the ctypesVehicle and the fieldsVehicle folders to the features folder on your SharePoint server….

 

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

 

After you have copied the folders you should install and activate the features, starting with the site columns. You can follow these instructions for installing/activating content types via the command line…

 

http://msdn2.microsoft.com/en-us/library/ms442691.aspx

 

or if your not a big command line fan you can use this nice little windows app to install them…

 

SharePoint Feature Manager - http://www.sharepointblogs.com/tbaginski/archive/2006/06/16/8423.aspx

 

Hope this article helps. Please feel free to send me your questions/corrections/suggestions.


-Mark


반응형