Friday, October 28, 2011

Rating in SharePoint 2010

I have moved to a new location. Please check out here.

21 comments:

Anonymous said...

Thanks for posting this Shiv, I've got an immediate use for this.

Cheers mate,
Jags

Shiv said...

@Jags,I am glad that it helped you.

Cheers,
Shiv

Krishna said...

Hi shiv,
Your post got very helpfull for me.In my case i was fetching my rating information from current web and sub webs also.But i got struck with "Httpcontext" when my current rating information is from other web other then current.so how to over come this.

Shiv said...

Krishna,
I am not getting the clear idea of what you're trying to resolve. I am afraid I may not have a definite answer to your question. However, you can see the code of the AverageRatingFieldControl in .Net Reflector and check if you get any idea there.

Anonymous said...

Hi Shiv, thanks for sharing this post. I'm having a slight issue and hoping you will be able to help out. I created a webpart (not visual webpart), looping through list items and creating AverageRatingFieldControl for each item. Also I've make sure ItemContext are assigned. Webpart render properly and showing list items with ratings for all items. My issue is I can only edit the rating for the first item and not the rest.

I tried assigning new ratingCntrl.ID = "RatingCntrl" + listItemID still no luck. Any advice on what I did wrong?

Anonymous said...

Please ignore my previous comment. Typo on my code and things are working now. Once again, thanks for your code/

Anonymous said...

Please ignore my previous comment. Typo on my code and things are working now. Once again, thanks for your code

Anonymous said...

hi Shiv, where listItemID come from?

Neven said...

Hi, I have the same problem here. My SPList and SPListItem are in the RootWeb. When I deploy webpart on the root, everything works. When I try to deploy webpart on some root subsite, I can't get current values for rating control.
I changed this two lines:
SPList reportList = SPContext.Current.Web.ParentWeb.Lists["Somelist"];
ratingCntrl.ItemContext = SPContext.GetContext(context, listItemId, reportList.ID, SPContext.Current.Web.ParentWeb);
but no luck.

Anonymous said...

Thank you very much shiv it helped a lot.

Unknown said...

Hello,

I have a solution for the subsite issue. I have decompiled the AverageRatingFieldControl, here are the code block which creates the item rating url:

if (string.IsNullOrEmpty(this.m_UrlForScript) && base.ItemContext != null)
{
string listItemServerRelativeUrl = base.ItemContext.ListItemServerRelativeUrl;
if (!string.IsNullOrEmpty(listItemServerRelativeUrl))
{
if (!listItemServerRelativeUrl.EndsWith(".000", StringComparison.OrdinalIgnoreCase))
{
listItemServerRelativeUrl = SPContext.Current.Web.Site.MakeFullUrl(listItemServerRelativeUrl);
}
else
{
SPWeb web = SPContext.Current.Web;
string str = "/_layouts/";
Guid listId = base.ListId;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);
string str1 = string.Concat(web.Url, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
listItemServerRelativeUrl = string.Concat(str2, "&ID=", base.ItemId);
}
this.m_UrlForScript = SPHttpUtility.EcmaScriptStringLiteralEncode(listItemServerRelativeUrl);
}
}
return this.m_UrlForScript;

The issue depends on the line "SPWeb web = SPContext.Current.Web;". The Current.Web is not always the web of the list item,
so in my webpart I use the Web of the List and not the current web

Here are my modified configuration for the AveageRatingControl:

HttpContext context = HttpContext.Current;
ratingCntrl.ListId = myList.ID;
ratingCntrl.ControlMode = SPControlMode.Edit;
ratingCntrl.ListId = myList.ID;

SPContext itemContext = SPContext.GetContext(context, myItem.ID, myList.ID, myList.ParentWeb);

SPWeb web = myList.ParentWeb;
string str = "/_layouts/";
Guid listId = myList.ID;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);

string serverUri = string.Empty;
if (!web.IsRootWeb)
{
serverUri = web.ServerRelativeUrl;
}

string str1 = string.Concat(serverUri, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
itemContext.ListItemServerRelativeUrl = string.Concat(str2, "&ID=", myItem.ID);

ratingCntrl.ItemContext = itemContext;

This works very well for me!

Best regards,
Jonas

Unknown said...

Hello,

I have a solution for the subsite issue. I have decompiled the AverageRatingFieldControl, here are the code block which creates the item rating url:

if (string.IsNullOrEmpty(this.m_UrlForScript) && base.ItemContext != null)
{
string listItemServerRelativeUrl = base.ItemContext.ListItemServerRelativeUrl;
if (!string.IsNullOrEmpty(listItemServerRelativeUrl))
{
if (!listItemServerRelativeUrl.EndsWith(".000", StringComparison.OrdinalIgnoreCase))
{
listItemServerRelativeUrl = SPContext.Current.Web.Site.MakeFullUrl(listItemServerRelativeUrl);
}
else
{
SPWeb web = SPContext.Current.Web;
string str = "/_layouts/";
Guid listId = base.ListId;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);
string str1 = string.Concat(web.Url, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
listItemServerRelativeUrl = string.Concat(str2, "&ID=", base.ItemId);
}
this.m_UrlForScript = SPHttpUtility.EcmaScriptStringLiteralEncode(listItemServerRelativeUrl);
}
}
return this.m_UrlForScript;

The issue depends on the line "SPWeb web = SPContext.Current.Web;". The Current.Web is not always the web of the list item,
so in my webpart I use the Web of the List and not the current web

Here are my modified configuration for the AveageRatingControl:

HttpContext context = HttpContext.Current;
ratingCntrl.ListId = myList.ID;
ratingCntrl.ControlMode = SPControlMode.Edit;
ratingCntrl.ListId = myList.ID;

SPContext itemContext = SPContext.GetContext(context, myItem.ID, myList.ID, myList.ParentWeb);

SPWeb web = myList.ParentWeb;
string str = "/_layouts/";
Guid listId = myList.ID;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);

string serverUri = string.Empty;
if (!web.IsRootWeb)
{
serverUri = web.ServerRelativeUrl;
}

string str1 = string.Concat(serverUri, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
itemContext.ListItemServerRelativeUrl = string.Concat(str2, "&ID=", myItem.ID);

ratingCntrl.ItemContext = itemContext;

This works very well for me!

Best regards,
Jonas

Unknown said...

Hello,

I have a solution for the subsite issue. I have decompiled the AverageRatingFieldControl, here are the code block which creates the item rating url:

if (string.IsNullOrEmpty(this.m_UrlForScript) && base.ItemContext != null)
{
string listItemServerRelativeUrl = base.ItemContext.ListItemServerRelativeUrl;
if (!string.IsNullOrEmpty(listItemServerRelativeUrl))
{
if (!listItemServerRelativeUrl.EndsWith(".000", StringComparison.OrdinalIgnoreCase))
{
listItemServerRelativeUrl = SPContext.Current.Web.Site.MakeFullUrl(listItemServerRelativeUrl);
}
else
{
SPWeb web = SPContext.Current.Web;
string str = "/_layouts/";
Guid listId = base.ListId;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);
string str1 = string.Concat(web.Url, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
listItemServerRelativeUrl = string.Concat(str2, "&ID=", base.ItemId);
}
this.m_UrlForScript = SPHttpUtility.EcmaScriptStringLiteralEncode(listItemServerRelativeUrl);
}
}
return this.m_UrlForScript;

The issue depends on the line "SPWeb web = SPContext.Current.Web;". The Current.Web is not always the web of the list item,
so in my webpart I use the Web of the List and not the current web

Here are my modified configuration for the AveageRatingControl:

HttpContext context = HttpContext.Current;
ratingCntrl.ListId = myList.ID;
ratingCntrl.ControlMode = SPControlMode.Edit;
ratingCntrl.ListId = myList.ID;

SPContext itemContext = SPContext.GetContext(context, myItem.ID, myList.ID, myList.ParentWeb);

SPWeb web = myList.ParentWeb;
string str = "/_layouts/";
Guid listId = myList.ID;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);

string serverUri = string.Empty;
if (!web.IsRootWeb)
{
serverUri = web.ServerRelativeUrl;
}

string str1 = string.Concat(serverUri, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
itemContext.ListItemServerRelativeUrl = string.Concat(str2, "&ID=", myItem.ID);

ratingCntrl.ItemContext = itemContext;

This works very well for me!

Best regards,
Jonas

Unknown said...

Hello,

I have a solution for the subsite issue. I have decompiled the AverageRatingFieldControl, here are the code block which creates the item rating url:

if (string.IsNullOrEmpty(this.m_UrlForScript) && base.ItemContext != null)
{
string listItemServerRelativeUrl = base.ItemContext.ListItemServerRelativeUrl;
if (!string.IsNullOrEmpty(listItemServerRelativeUrl))
{
if (!listItemServerRelativeUrl.EndsWith(".000", StringComparison.OrdinalIgnoreCase))
{
listItemServerRelativeUrl = SPContext.Current.Web.Site.MakeFullUrl(listItemServerRelativeUrl);
}
else
{
SPWeb web = SPContext.Current.Web;
string str = "/_layouts/";
Guid listId = base.ListId;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);
string str1 = string.Concat(web.Url, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
listItemServerRelativeUrl = string.Concat(str2, "&ID=", base.ItemId);
}
this.m_UrlForScript = SPHttpUtility.EcmaScriptStringLiteralEncode(listItemServerRelativeUrl);
}
}
return this.m_UrlForScript;

The issue depends on the line "SPWeb web = SPContext.Current.Web;". The Current.Web is not always the web of the list item,
so in my webpart I use the Web of the List and not the current web

Here are my modified configuration for the AveageRatingControl:

HttpContext context = HttpContext.Current;
ratingCntrl.ListId = myList.ID;
ratingCntrl.ControlMode = SPControlMode.Edit;
ratingCntrl.ListId = myList.ID;

SPContext itemContext = SPContext.GetContext(context, myItem.ID, myList.ID, myList.ParentWeb);

SPWeb web = myList.ParentWeb;
string str = "/_layouts/";
Guid listId = myList.ID;
string upper = listId.ToString("B").ToUpper(CultureInfo.InvariantCulture);

string serverUri = string.Empty;
if (!web.IsRootWeb)
{
serverUri = web.ServerRelativeUrl;
}

string str1 = string.Concat(serverUri, str, "listform.aspx?PageType=");
string str2 = string.Concat(str1, "4&ListId=", upper);
str2 = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(str2, true));
itemContext.ListItemServerRelativeUrl = string.Concat(str2, "&ID=", myItem.ID);

ratingCntrl.ItemContext = itemContext;

This works very well for me!

Best regards,
Jonas

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Anonymous said...

Hi Shiva,
Thank you very much for the nice article. It helped me a lot.
I have used this rating control on a SPGridView in a webaprt.
Everything works fine, but when I put this SPGridView in an UpdatePanel, this starts conflicting with update panel. On close examination, I found that there is some exception thrown, in rating.js file in layouts folder which SharePoint uses fro this control.

If anyone has encountered this or any help will be highly appreciated.

Thanks

Unknown said...

Thanks for sharing shiv,Its really very useful to me and I learned a new things from your post.
Consulting Firms in India