Bilbro Bloggins

Musings of Brian Bilbro

Adding Target Audience in SharePoint with VSeWSS

If you're like me and are fairly new to SharePoint you tend to rely on the Visual Studio extensions for Windows SharePoint Services.   A lot of the examples out there assume you have a certain knowledge level or experience with SharePoint and therefore they can be confusing.  I’ll discuss step-by-step how to set the Target Audience while using VSeWSS.

All-in-all though, this blog entry will cover multiple topics:

  1. Creating a blank site definition.
  2. Adding a Content Type to that definition.
  3. Setting the Target Audience to that Content Type.
  4. Creating a List Definition from that Content Type.
  5. Creating an instance of that list.

 

Step 1 – In Visual Studio, create a Blank Site Definition.

Step01.CreateBlankSite

 

Step 2 – After the project is created.  Add a Content Type to the project.  I like to keep my folders somewhat organized so I’ll create a ContentTypes folder first.

A. Right Click Project->Add->New Folder

Name the folder ContentTypes

B. Right Click ContentTypes folder->Add->New Item->SharePoint->Content Type

Enter a name for your content and select Add.

Step02.a.AddContentType

For the Base Content Type dialog select “item” and then OK.

Step02.b.SelectBaseContentType

 

Step 3 – Now lets add two fields to our content type.  A note type field (multiline) and the target audience field. Open up the content type xml file that was created by Visual Studio.

In the <FieldsRefs> node add the following:

<FieldRefs>
  <FieldRef ID="{3FA6CCEC-8798-4cd1-A077-13316657D208}" Name="SampleDetailsField" />
  <FieldRef ID="{61cbb965-1e04-4273-b658-eedaa662f48d}" Name="Target_x0020_Audiences" />
</FieldRefs>

It’s important you use the GUID {61cbb965-1e04-4273-b658-eedaa662f48d} for the Target Audience.   SharePoint seems to expect that particular GUID (and not some randomly generated GUID).   If you generate your own guild, SharePoint will create the field and it will look like the Target Audience field but won’t act like the Target Audience field.  Seems to be one of those quirks when dealing with SharePoint.

Within the <Elements> node add the following items:

<Field ID="{3FA6CCEC-8798-4cd1-A077-13316657D208}"
       Type="Note"
       Name="SampleDetailsField"
       DisplayName="Details"
       Hidden="FALSE"
       Required="FALSE"
       Sealed="FALSE"
       RichText="TRUE"
       DisplaceOnUpgrade="TRUE"
       RichTextMode = "FullHtml" />
<Field ID="{61cbb965-1e04-4273-b658-eedaa662f48d}"
       Type="TargetTo"
        Name="Target_x0020_Audiences"
        DisplayName="Target Audiences"
        Required="FALSE"
        StaticName="Target_x0020_Audiences"
    />
As before, make sure the Target Audience GUID is specifically  {61cbb965-1e04-4273-b658-eedaa662f48d}.

Your completed xml file should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="2efb2773-41e5-44a6-b580-c61b2e42ab60" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x01006e1f74248084402cbbefbd010e8a3cfb"
               Name="BlogSampleContentType"
               Group="Development"
               Description="Developing Content Type"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{3FA6CCEC-8798-4cd1-A077-13316657D208}" Name="SampleDetailsField" />
      <FieldRef ID="{61cbb965-1e04-4273-b658-eedaa662f48d}" Name="Target_x0020_Audiences" />
    </FieldRefs>
  </ContentType>
  <Field ID="{3FA6CCEC-8798-4cd1-A077-13316657D208}"
         Type="Note"
         Name="SampleDetailsField"
         DisplayName="Details"
         Hidden="FALSE"
         Required="FALSE"
         Sealed="FALSE"
         RichText="TRUE"
         DisplaceOnUpgrade="TRUE"
         RichTextMode = "FullHtml" />
  <Field ID="{61cbb965-1e04-4273-b658-eedaa662f48d}"
         Type="TargetTo"
          Name="Target_x0020_Audiences"
          DisplayName="Target Audiences"
          Required="FALSE"
          StaticName="Target_x0020_Audiences"
      />
</Elements>

 

Step 4 – Add the List Definition.  As before with the Content Types, let’s create a folder to hold our list definitions and keep everything nice and neat.

A. Right Click Project->Add->New Folder

Name the folder ListDefinitions

B. Right Click the ListDefinitions folder->Add->New Item->SharePoint->List Definitions from Content Type

Step04.a.AddListDefinition

Select the content type you created in Step 3.  And check the option “Create an instance of this list”.

Step04.b.SelectBaseContentType

A new folder should be created with 4 aspx files (AllItems, DispForm, EditForm and NewForm) and 3 xml files (instance, ListDefinition and schema).

 

Step 5 – Change the name of the list instance.

I don’t particularly like the default list instance name that is provided.  It will be the name of your list definition with the word instance tacked on.  Open the file instance.xml and modify the Title attribute of the ListInstance node to something you would like better.

<ListInstance FeatureId="81073dbb-2946-4b8e-b273-0d36a70bd505" 
              Title="MySampleList" 
              Url="Lists/SampleListDefinition">
</ListInstance>

 

Step 6 – Deploy your new Site Definition.

A. Set the URL to your SharePoint server:  Right click project -> Properties -> Debug -> Start browser with URL.

Step06.a.WebPartProperties

Step06.b.DebugURL

B. Build and Deploy the web part to your SharePoint server. Right click project –> Deploy

 

Step 7 – Create a new site based upon your newly created Site Definition.

A. Open up your SharePoint portal in your browser.

B. Select Site Actions->Site Settings

Step07.a.SiteSettings

C. Select “Sites and Workspaces” under the Site Administration section.

D. Select Create

E. Enter a Title, Description and URL.

F. Select the “Development” tab for the “Select a template” section.  You should see your newly created Site Definition listed.

Step07.b.CreateASite

G. Select “Create”.

SharePoint will spin and churn for a bit and eventually it will take you to your newly created site.

Step 8 – Verify your list was created and Audience Targeting was turned on.

A. From the left-hand menu options “View All Site Content”, select Lists.

Step08.a.SelectLists

B. Your list instance should show up as one of the lists.  Select your list by left-clicking on it’s name.

C. Select New->New Item

You should see the Target Audiences field.

Step08.b.NewItem

 

You can download the complete source code from CodePlex:

http://www.codeplex.com/BilbroBloggins/

» Similar Posts

  1. Creating a Lookup List for SharePoint with VSeWSS
  2. Yes, Virginia, you can write through the BDC – Part One.
  3. Yes, Virginia, you can write through the BDC – Part Two.

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://bilbroblog.com/trackback.ashx?id=11
 

» Comments

  1. vijay sathe avatar

    The download link is not available. Can you please a link from where I can download source code.

    vijay sathe — January 14, 2010 3:59 AM

» Leave a Comment