/// <summary>
        /// Enable Social Settings Likes/Ratings on list. 
        /// Note: 1. Requires Publishing feature enabled on the web.
        ///       2. Defaults enable Ratings Experience on the List
        /// </summary>
        /// <param name="list">Current List</param>
        /// <param name="experience">Likes/Ratings</param>
        public static void SetRating(this List list, VotingExperience experience)
        {
            /*  Validate if current web is publishing web
             *  Add property to RootFolder of List/Library : key: Ratings_VotingExperience value:Likes
             *  Add rating fields
             *  Add fields to default view
             * */

            _library = list;

            //  only process publishing web
            if (!list.ParentWeb.IsPublishingWeb())
            {
                throw new Exception("Not publishing web");
                ////_logger.WriteWarning("Is publishing site : No");
            }

            //  Add to property Root Folder of Pages Library
            AddProperty(experience);

            AddListFields();

            AddViewFields(experience);

            //_logger.WriteSuccess(string.Format("Enabled {0} on list/library {1}", experience, _library.Title));
        }
        /// <summary>
        /// Enable Social Settings Likes/Ratings on list. 
        /// Note: 1. Requires Publishing feature enabled on the web.
        ///       2. Defaults enable Ratings Experience on the List
        ///       3. When experience set to None, fields are not removed from the list since CSOM does not support removing hidden fields
        /// </summary>
        /// <param name="list">Current List</param>
        /// <param name="experience">Likes/Ratings</param>
        public static void SetRating(this List list, VotingExperience experience)
        {
            /*  Validate if current web is publishing web
             *  Add property to RootFolder of List/Library : key: Ratings_VotingExperience value:Likes
             *  Add rating fields
             *  Add fields to default view
             * */

            _library = list;

            //  only process publishing web
            if (!list.ParentWeb.IsPublishingWeb())
            {
                ////_logger.WriteWarning("Is publishing site : No");
            }

            //  Add to property Root Folder of Pages Library
            SetProperty(experience);

            if (experience == VotingExperience.None)
            {
                RemoveViewFields();
            }
            else
            {
                AddListFields();
                // remove already existing fields from the default view to prevent duplicates
                RemoveViewFields();
                AddViewFields(experience);
            }

            //_logger.WriteSuccess(string.Format("Enabled {0} on list/library {1}", experience, _library.Title));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Enable Social Settings Likes/Ratings on given list
        /// </summary>
        /// <param name="listName">List Name</param>
        /// <param name="experience">Likes/Ratings</param>
        public void Enable(string listName, VotingExperience experience)
        {
            /*  Get root Web
             *  Validate if current web is publishing web
             *  Find List/Library library
             *  Add property to RootFolder of List/Library : key: Ratings_VotingExperience value:Likes
             *  Add rating fields
             *  Add fields to default view
             * */

            var web = _context.Site.RootWeb;

            _context.Load(_context.Site.RootWeb, p => p.Url);
            _context.ExecuteQuery();

            try
            {
                _logger.WriteInfo("Processing: " + web.Url);
                EnsureReputation(web, listName, experience);
            }
            catch (Exception e)
            {
                _logger.WriteException(string.Format("Error: {0} \nMessage: {1} \nStack: {2}", web.Url, e.Message, e.StackTrace));
            }
        }
        /// <summary>
        /// Add/Remove Ratings/Likes field in default view depending on exerpeince selected
        /// </summary>
        /// <param name="experience"></param>
        private static void AddViewFields(VotingExperience experience)
        {
            //  Add LikesCount and LikeBy (Explicit) to view fields
            _library.Context.Load(_library.DefaultView, p => p.ViewFields);
            _library.Context.ExecuteQueryRetry();

            var defaultView = _library.DefaultView;

            switch (experience)
            {
                case VotingExperience.Ratings:
                    //  Remove Likes Fields
                    if (defaultView.ViewFields.Contains("LikesCount"))
                        defaultView.ViewFields.Remove("LikesCount");

                    defaultView.ViewFields.Add("AverageRating");
                    //  Add Ratings related field
                    break;
                case VotingExperience.Likes:
                    //  Remove Ratings Fields
                    //  Add Likes related field
                    if (defaultView.ViewFields.Contains("AverageRating"))
                        defaultView.ViewFields.Remove("AverageRating");

                    defaultView.ViewFields.Add("LikesCount");
                    break;
                default:
                    throw new ArgumentOutOfRangeException("experience");
            }

            defaultView.Update();
            _library.Context.ExecuteQueryRetry();
            //_logger.WriteSuccess("Ensured view-field.");

        }
Ejemplo n.º 5
0
        /// <summary>
        /// Enable Social Settings Likes/Ratings on list.
        /// Note: 1. Requires Publishing feature enabled on the web.
        ///       2. Defaults enable Ratings Experience on the List
        ///       3. When experience set to None, fields are not removed from the list since CSOM does not support removing hidden fields
        /// </summary>
        /// <param name="list">Current List</param>
        /// <param name="experience">Likes/Ratings</param>
        public static void SetRating(this List list, VotingExperience experience)
        {
            /*  Validate if current web is publishing web
             *  Add property to RootFolder of List/Library : key: Ratings_VotingExperience value:Likes
             *  Add rating fields
             *  Add fields to default view
             * */

            _library = list;

            //  only process publishing web
            if (!list.ParentWeb.IsPublishingWeb())
            {
                throw new Exception("Not publishing web");
                ////_logger.WriteWarning("Is publishing site : No");
            }

            //  Add to property Root Folder of Pages Library
            SetProperty(experience);

            if (experience == VotingExperience.None)
            {
                RemoveViewFields();
            }
            else
            {
                AddListFields();

                AddViewFields(experience);
            }

            //_logger.WriteSuccess(string.Format("Enabled {0} on list/library {1}", experience, _library.Title));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Add required key/value settings on List Root-Folder
        /// </summary>
        /// <param name="experience"></param>
        private static void AddProperty(VotingExperience experience)
        {
            _library.Context.Load(_library.RootFolder, p => p.Properties);
            _library.Context.ExecuteQueryRetry();

            _library.RootFolder.Properties["Ratings_VotingExperience"] = experience.ToString();
            _library.RootFolder.Update();
            _library.Context.ExecuteQueryRetry();
            //_logger.WriteSuccess(string.Format("Ensured {0} Property.", experience));
        }
        /// <summary>
        /// Validate if the RootFolder property is set appropriately
        /// </summary>
        /// <returns></returns>
        private bool IsRootFolderPropertySet(VotingExperience experience = VotingExperience.Ratings)
        {
            _clientContext.Load(_list.RootFolder.Properties);
            _clientContext.ExecuteQueryRetry();

            if (_list.RootFolder.Properties.FieldValues.ContainsKey(RatingsVotingexperience))
            {
                object exp;
                if (_list.RootFolder.Properties.FieldValues.TryGetValue(RatingsVotingexperience, out exp))
                {
                    return(string.Compare(exp.ToString(), experience.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0);
                }
            }

            return(false);
        }
        /// <summary>
        /// Validate if required experience selected fields are added to default view
        /// </summary>
        /// <param name="experience"></param>
        /// <returns></returns>
        private bool RatingFieldSetOnDefaultView(VotingExperience experience = VotingExperience.Ratings)
        {
            _clientContext.Load(_list.DefaultView.ViewFields);
            _clientContext.ExecuteQueryRetry();

            switch (experience)
            {
            case VotingExperience.Ratings:
                return(_list.DefaultView.ViewFields.Contains(Averagerating));

            case VotingExperience.Likes:
                return(_list.DefaultView.ViewFields.Contains(Likescount));

            default:
                throw new ArgumentOutOfRangeException("experience");
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Add required key/value settings on List Root-Folder
        /// </summary>
        /// <param name="experience"></param>
        private void AddProperty(VotingExperience experience)
        {
            _context.Load(_library.RootFolder, p => p.Properties);
            _context.ExecuteQuery();

            if (experience != VotingExperience.None)
            {
                _library.RootFolder.Properties["Ratings_VotingExperience"] = experience.ToString();
            }
            else
            {
                _library.RootFolder.Properties["Ratings_VotingExperience"] = "";
            }

            _library.RootFolder.Update();
            _context.ExecuteQuery();
            _logger.WriteSuccess(string.Format("Ensured {0} Property.", experience));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Add/Remove Ratings/Likes field in default view depending on exerpeince selected
        /// </summary>
        /// <param name="experience"></param>
        private static void AddViewFields(VotingExperience experience)
        {
            //  Add LikesCount and LikeBy (Explicit) to view fields
            _library.Context.Load(_library.DefaultView, p => p.ViewFields);
            _library.Context.ExecuteQueryRetry();

            var defaultView = _library.DefaultView;

            switch (experience)
            {
            case VotingExperience.Ratings:
                //  Remove Likes Fields
                if (defaultView.ViewFields.Contains("LikesCount"))
                {
                    defaultView.ViewFields.Remove("LikesCount");
                }

                defaultView.ViewFields.Add("AverageRating");
                //  Add Ratings related field
                break;

            case VotingExperience.Likes:
                //  Remove Ratings Fields
                //  Add Likes related field
                if (defaultView.ViewFields.Contains("AverageRating"))
                {
                    defaultView.ViewFields.Remove("AverageRating");
                }

                defaultView.ViewFields.Add("LikesCount");
                break;

            default:
                throw new ArgumentOutOfRangeException("experience");
            }

            defaultView.Update();
            _library.Context.ExecuteQueryRetry();
            //_logger.WriteSuccess("Ensured view-field.");
        }
Ejemplo n.º 11
0
        private void EnsureReputation(Web web,string listName,VotingExperience experience)
        {
            //  only process publishing web
            if (!IsPublishingWeb(web))
            {
                _logger.WriteWarning("Is publishing site : No");
                return;
            }

            _logger.WriteInfo("Is publishing site : Yes");

            SetCulture(GetWebCulture(web));

            //  Fetch Library
            try
            {
                _library = web.Lists.GetByTitle(listName);
                _context.Load(_library);
                _context.ExecuteQuery();

                _logger.WriteSuccess("Found list/library : " + _library.Title);
            }
            catch (ServerException e)
            {
                _logger.WriteException(string.Format("Error: {0} \nMessage: {1} \nStack: {2}", web.Url, e.Message, e.StackTrace));
                return;
            }

            //  Add to property Root Folder of Pages Library
            AddProperty(experience);

            AddListFields();

            AddViewFields(experience);

            _logger.WriteSuccess(string.Format("Enabled {0} on list/library {1}", experience,_library.Title));
        }
Ejemplo n.º 12
0
        private void EnsureReputation(Web web, string listName, VotingExperience experience)
        {
            //  only process publishing web
            if (!IsPublishingWeb(web))
            {
                _logger.WriteWarning("Is publishing site : No");
                return;
            }

            _logger.WriteInfo("Is publishing site : Yes");

            SetCulture(GetWebCulture(web));

            //  Fetch Library
            try
            {
                _library = web.Lists.GetByTitle(listName);
                _context.Load(_library);
                _context.ExecuteQuery();

                _logger.WriteSuccess("Found list/library : " + _library.Title);
            }
            catch (ServerException e)
            {
                _logger.WriteException(string.Format("Error: {0} \nMessage: {1} \nStack: {2}", web.Url, e.Message, e.StackTrace));
                return;
            }

            //  Add to property Root Folder of Pages Library
            AddProperty(experience);

            AddListFields();

            AddViewFields(experience);

            _logger.WriteSuccess(string.Format("Enabled {0} on list/library {1}", experience, _library.Title));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Enable Social Settings Likes/Ratings on given list
        /// </summary>
        /// <param name="listName">List Name</param>
        /// <param name="experience">Likes/Ratings</param>
        public void Enable(string listName,VotingExperience experience)
        {
            /*  Get root Web
             *  Validate if current web is publishing web
             *  Find List/Library library 
             *  Add property to RootFolder of List/Library : key: Ratings_VotingExperience value:Likes
             *  Add rating fields
             *  Add fields to default view
             * */

            var web = _context.Site.RootWeb;
            _context.Load(_context.Site.RootWeb, p => p.Url);
            _context.ExecuteQuery();

            try
            {
                _logger.WriteInfo("Processing: " + web.Url);
                EnsureReputation(web, listName,experience);
            }
            catch (Exception e)
            {
                _logger.WriteException(string.Format("Error: {0} \nMessage: {1} \nStack: {2}", web.Url, e.Message, e.StackTrace));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Add required key/value settings on List Root-Folder
        /// </summary>
        /// <param name="experience"></param>
        private void AddProperty(VotingExperience experience)
        {
            _context.Load(_library.RootFolder, p => p.Properties);
            _context.ExecuteQuery();

            if (experience != VotingExperience.None)
            {
                _library.RootFolder.Properties["Ratings_VotingExperience"] = experience.ToString();
            }
            else
            {
                _library.RootFolder.Properties["Ratings_VotingExperience"] = "";
            }
            
            _library.RootFolder.Update();
            _context.ExecuteQuery();
            _logger.WriteSuccess(string.Format("Ensured {0} Property.",experience));
        }
        /// <summary>
        /// Validate if the RootFolder property is set appropriately
        /// </summary>
        /// <returns></returns>
        private bool IsRootFolderPropertySet(VotingExperience experience = VotingExperience.Ratings)
        {
            _clientContext.Load(_list.RootFolder.Properties);
            _clientContext.ExecuteQueryRetry();

            if (_list.RootFolder.Properties.FieldValues.ContainsKey(RatingsVotingexperience))
            {
                object exp;
                if (_list.RootFolder.Properties.FieldValues.TryGetValue(RatingsVotingexperience, out exp))
                {
                    return string.Compare(exp.ToString(), experience.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0;
                }
            }

            return false;
        }
Ejemplo n.º 16
0
 public RatingAttribute(VotingExperience votingExperience)
 {
     VotingExperience = (int)votingExperience;
 }
        /// <summary>
        /// Validate if required experience selected fields are added to default view
        /// </summary>
        /// <param name="experience"></param>
        /// <returns></returns>
        private bool RatingFieldSetOnDefaultView(VotingExperience experience = VotingExperience.Ratings)
        {
            _clientContext.Load(_list.DefaultView.ViewFields);
            _clientContext.ExecuteQueryRetry();

            switch (experience)
            {
                case VotingExperience.Ratings:
                    return _list.DefaultView.ViewFields.Contains(Averagerating);
                case VotingExperience.Likes:
                    return _list.DefaultView.ViewFields.Contains(Likescount);
                default:
                    throw new ArgumentOutOfRangeException("experience");
            }
        }
        /// <summary>
        /// Add required key/value settings on List Root-Folder
        /// </summary>
        /// <param name="experience"></param>
        private static void AddProperty(VotingExperience experience)
        {
            _library.Context.Load(_library.RootFolder, p => p.Properties);
            _library.Context.ExecuteQueryRetry();

            _library.RootFolder.Properties["Ratings_VotingExperience"] = experience.ToString();
            _library.RootFolder.Update();
            _library.Context.ExecuteQueryRetry();
            //_logger.WriteSuccess(string.Format("Ensured {0} Property.", experience));
        }