Friday, March 8, 2013

Add rating Field To Discussion board


This Code To Add Rating Field To Discussion Board List
=================================


private void  DiscussionBoardListsOperation(SPWeb oSite)
        {          
            SPSecurity.RunWithElevatedPrivileges(delegate                {                    oSite.AllowUnsafeUpdates = true;                    SPListCollectionAdapter listAdapter = new SPListCollectionAdapter(oSite.Lists);                    var result = from list in listAdapter                                 where list.BaseTemplate.Equals(SPListTemplateType.DiscussionBoard)                                 select list;
                    foreach (var disclist in result)                    {                        ActivateRatingForList(disclist);                    }                    oSite.AllowUnsafeUpdates = false;                });        }     
        public void ActivateRatingForList(SPList list)        {            SPView DiscSubView = list.Views["Subject"];            SPView DiscFlatView = list.Views["Flat"];            SPView DiscThreadViews = list.Views["Threaded"];            Guid averageRatingId = new Guid("5a14d1ab-1513-48c7-97b3-657a5ba6c742");            Guid ratingCountId = new Guid("b1996002-9167-45e5-a4df-b2c41c6723c7");
            SPField averageRating = list.ParentWeb.AvailableFields[averageRatingId];            if (averageRating != null && !list.Fields.Contains(averageRating.Id))            {                list.Fields.Add(averageRating);            }            SPField ratingCount = list.ParentWeb.AvailableFields[ratingCountId];            if (ratingCount != null && !list.Fields.Contains(ratingCount.Id))            {                list.Fields.Add(ratingCount);            }            if (averageRating != null && !DiscSubView.ViewFields.Exists(averageRating.InternalName))            DiscSubView.ViewFields.Add(averageRating);          
            if (averageRating != null && !DiscFlatView.ViewFields.Exists(averageRating.InternalName))            DiscFlatView.ViewFields.Add(averageRating);
            string query = @"<OrderBy>                                    <FieldRef Name='Created' Ascending='FALSE' />                                    <FieldRef Name='ItemChildCount' Ascending='FALSE' />                                </OrderBy>";
            DiscSubView.Paged = true;            DiscSubView.RowLimit = 10;
            DiscFlatView.Paged = true;            DiscFlatView.RowLimit = 10;
            DiscThreadViews.Paged = true;            DiscThreadViews.RowLimit = 10;
            DiscFlatView.Query = query;            DiscSubView.Query = query;            DiscThreadViews.Query = query;          
            DiscFlatView.Update();            DiscSubView.Update();            DiscThreadViews.Update();          
            list.Update();        }

No comments:

Post a Comment