Tuesday, September 27, 2011

Sharepoint 2010 Adding a "Publishing Image" to a Sharepoint List programmatically.

What is "Publishing Image" content type?

"Publishing Image" is a content type which will bind the image with all its properties and will show it on the screen with those properties applied. The properties like height width etc.


What my task was?

My task was to add a "Publishing Image" content type with Display Name as "Rollup Image" in the content type list "Post". This was a site level feature, once i activate this feature all the blog type site with SharePoint List "Post" will have an extra field "Rollup Image". Please have a look at the 2 pictures which can give you a clear idea.


The 1st from the above one is new post screen. and the 2nd one comes when you click to add a new image reference.


Solution

  1. Open a visual studio 2010
  2. Add new project
  3. Empty Sharepoint Project > Deploy as Farm Solution > Finish.
  4. In the visual studio solution project select "Features > Feature1 > Feature1.feature".
  5. Right click "Add event receiver".
  6. I used this code to achieve it.

    /// <summary>
    /// Add a field name "Rollup Image" to a content type "Post".
    /// "Post" is the content type used in the Site Template blog.
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
            {
                //Post is the content type list which is on the blogs.
                SPContentType _contentType = _web.ContentTypes["Post"];
    
                if (_contentType != null)
                {
                    //PublishingRollupImage is the static name for the "Publishing Image "
                    if (!_contentType.Fields.ContainsFieldWithStaticName("PublishingRollupImage"))
                    {
                        //Name of the column to be added to the list content type post
                        SPField _field = _web.AvailableFields["Rollup Image"];
                        SPFieldLink _fieldText = new SPFieldLink(_field);
    
                        if (_contentType.FieldLinks[_fieldText.Id] == null)
                        {
                            _contentType.FieldLinks.Add(_fieldText);
                        }
                        _contentType.Update(true);
                    }
                }
            }
        });
    }
  7. Deploy the solution.




How to verify this?

  1. Open the Sharepoint Site.
  2. Site Actions > Site Settings > Site Content Types
  3. Click "Post" in "List Content Type".
  4. You can see the newly added column in the image below. For checking the above 2 images you should go to the "Post" sharepoint list in the blog site.



Please feel free to ask question, I will be more glad to answer your question.

2 comments:

  1. After creating content type of type image how to view the image in the publishing site?It shows the Html tag.Can you help me out?

    ReplyDelete
  2. Hello buddy, Can you explain me your whole scenario? If you are using Content Query Web Part you can modify the xsl and use the @Image.Url. This property can give you the URL of Image.

    ReplyDelete