Archive

Posts Tagged ‘sharepoint rating’

Adding SharePoint 2010 rating control on your custom forms

February 5th, 2010

Here is an interesting question from a reader: I’m wondering, is there a way to use SharePoint rating control on a form (custom aspx page, which is in _layouts folder) in SharePoint 2010? If so, what’s the syntax? I found there is a control called RatingScaleField, not sure how to use it.

The answer is: maybe but it would require a lot of engineering around the rating control. Rating control is contextual to the item and it pulls/sends information about users rating to/from the SocialDatabase in SharePoint. If you want to use out of the box rating control on a separate page – it will have to have the context of an item you’re referring to.

One other approach to place out of the box rating control on a custom page is to create a new instance of Display/Edit form rendering template. Meaning that you can arrange your fields in any order you want or add new markup or new fields or ASPX controls to the page. Check out this article to get a general idea how to create your own rendering templates.

Once you have an idea about rendering templates, all of you need to do is to reference your rating field as a CompositeField on a rendering template, like this:

<SharePoint:CompositeField ID=”MyRatingFieldId” runat=’server’ FieldName=’Rating_x0020__x0028_0_x002d_5_x0′ />

This will generate a rating scale field on your form wherever you place it. All the wiring is going to be done as well so SharePoint will know where to send your rating etc.

If you want to know how one of my collegues create his own rating control that utilizes SharePoint 2010 SocialData back end – check out both parts of this article.

Good luck!

sharepoint, sharepoint 2010 , , , ,

Extending & troubleshooting SharePoint 2010 rating related features

January 19th, 2010

Last few weeks I have been crazy busy extending SharePoint 2010 rating control. As you know default SharePoiunt 2010 rating control is 5-star scale.

One of the tasks I had to do is to perform my own calculations based on each time the AverageRating has been updated. As you may know average rating gets calculated on a Job schedule defined in Central Administration. You can set that job to run every minute if you like. Once the job runs – it will take the infromation from rating database and place it to the content database thus showing average. The problem I faced is that my even handler – perfectly tied to a list and executing all of the updates I do on the list – wouldn’t trigget on rating updates.

Here is how to fix this issue:

By enabling rating in Lists Settings of your list – all that SharePoint does is creates 2 fields. You will need to set pushchangestolist property of 2 created fields (Rating (0-5)  and Rating Count) to true. The way I approached it – is simply by creating a feature that enables twose 2 fields and sets all of the appropriate values. Here is an article where you can get the code on how to do that.

Now your rating field changes will trigger events on the receiver. You probably want to know how to debug your code since attaching to W3P won’t work. Keep in mind that all of the events that are running on the schedule are running with OWSTIMER process, which is Windows SharePoint Services Timer. You want to attach your Visual Studio 2010 debugger to that. It won’t capture your other events such as page updates etc, but will capture job execution.

Good luck!

sharepoint, sharepoint 2010 , , , , ,

Programaticaly enable rating on SharePoint 2010 lists

December 31st, 2009

SharePoint 2010 Rating is a great new feature (I’m using SharePoint 2010 Beta 2).
If you’re reading this post you probably already know about its functionality and how to enable it through the GUI.
When you deploy your solution to a client environment you probably want it to be as packaged and scripted as possible and not have to send large depoyment instructions; so here we talk about how to enable a rating on a list using SharePoint 2010 Feature which can be automatically activated in your deployment script.

1. We’ll create a simple Empty SharePoint 2010 Solution in Visual Studio 2010 and add a new feature to it.
2. Add a feature receiver to your feature and uncomment FeatureActivated section.
3. Paste the code below into your FeatureActivated:

string listName = string.Empty;
string averageRatingId = string.Empty;
string ratingCountId = string.Empty;

try
            {
                // use any list name here that you want to enable rating on
                listName = “Posts”;
                // those are reserved column IDs
                averageRatingId = “5a14d1ab-1513-48c7-97b3-657a5ba6c742″; 
                ratingCountId = “b1996002-9167-45e5-a4df-b2c41c6723c7″;
                SPList list = ((SPWeb)properties.Feature.Parent).Lists[listName];
              
                SPField fieldByField = GetFieldById(new Guid(averageRatingId), list.ParentWeb.AvailableFields);
                if (fieldByField != null && !list.Fields.ContainsField(fieldByField.StaticName))
                {
                    list.Fields.AddFieldAsXml(fieldByField.SchemaXml, true, SPAddFieldOptions.AddFieldToDefaultView | SPAddFieldOptions.AddToAllContentTypes);
                }
               
                SPField ratingCountField = GetFieldById(new Guid(ratingCountId), list.ParentWeb.AvailableFields);
                if (ratingCountField != null && !list.Fields.ContainsField(ratingCountField.StaticName))
                {
                    list.Fields.AddFieldAsXml(ratingCountField.SchemaXml, false, SPAddFieldOptions.AddToAllContentTypes);
                }
                list.Update();
                Repropagate(list);
            }
            catch { }

Above code creates two fields in the Posts list, you can have any list name you like. Two fields I’m referring to are : Average Rating, and Number of Raters. Those have their corresponding types. Now, you’ll notice I used two helper functions:

1. GetFieldById - which will get a field name by guid passed
2. Repropagate – will propagate rating functionality to list items (otherwise they won’t get a rating stars beside them)

The following code fro those two functions in a helpers region of your code:

#region Helpers

        private static void Repropagate(SPList list)
        {
            SocialRatingManager ratingManager = new SocialRatingManager(SPServiceContext.Current);
            string baseUrl = list.ParentWeb.Url;
            if (baseUrl.EndsWith(”/”, StringComparison.OrdinalIgnoreCase))
            {
                baseUrl = baseUrl.TrimEnd(new char[] { ‘/’ });
            }
            foreach (SPListItem item in list.Items)
            {
                string itemUrl = item.Url;
                if (itemUrl.StartsWith(”/”, StringComparison.OrdinalIgnoreCase))
                {
                    itemUrl = itemUrl.TrimStart(new char[] { ‘/’ });
                }
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    ratingManager.PropagateRating(new Uri(baseUrl + “/” + itemUrl));
                });
            }
        }

        private static SPField GetFieldById(Guid id, SPFieldCollection fieldArray)
        {
            try
            {
                return fieldArray[id];
            }
            catch
            {
                return null;
            }
        }

        #endregion

 Note: You will need to add using Microsoft.Office.Server.SocialData;  to your feature. This using statement will require you to add the following assembly:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.UserProfiles.dll
This assembly depends on the correct version of Microsoft Chart Controls for Microsoft .NET Framework 3.5 DLL installed. You can reference the DLL directly in your solution by extracting it from you GAC. The assembly is called System.Web.DataVisualization

Now, the last part is to handle the feature being deactivated; all we need to do in this case is delete two column we have added. We can reuse the same code to get a hold of the list but instead of adding item we delete them:

if (fieldByField != null && list.Fields.ContainsField(fieldByField.StaticName))
                {
                    list.Fields.Delete(fieldByField.StaticName);
                }

That’s it. Now when you deploy your solution and the feature is installed and activated, ratings will be enabled on the Posts list in our case. Note that we executed AddFieldToDefaultView in our Feature Activated meaning that our rating stars will be displayed only on taht view. If you want them to appear on any other view – you can just add the Rating (0-5)  column to the view. Rating column is almost like any other column and you can sort and filter by it.

Enjoy!

sharepoint, sharepoint 2010 , , , , ,