Friday, July 29, 2011

SharePoint - Counting Social Tags

I found some code on the web for counting social tags, but unfortunately it failed when the page was a "welcome" page. The code below handles that special case. I used a Web Service Reference to the SocialDataService web service, although I probably could have created a (WCF) Service Reference instead.

The method IsWelcomePage() is detailed in my previous post. The method TrimPageFromUrl() is fairly simple, it just changes a URL such as http://spsite/pages/default.aspx to http://spsite/.

private void CountTagsByTermNameAndUrl()
{
using (SocDataSvc.SocialDataService socialDataService = new SocDataSvc.SocialDataService())
{
socialDataService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
socialDataService.Url = GetUrlWithUpdatedDomain(socialDataService.Url, m_siteUrl);
SocDataSvc.SocialTermDetail termDetail;
try
{
termDetail = socialDataService.GetTagTermsOnUrl(m_siteUrl, null)
.Where(detail => detail.Term.Name == m_termName).SingleOrDefault();
if (termDetail == null && IsWelcomePage(m_siteUrl))
{ // this could be the default page, remove the last part of the Url and try again
m_siteUrl = TrimPageFromUrl(m_siteUrl);
termDetail = socialDataService.GetTagTermsOnUrl(m_siteUrl, null)
.Where(detail => detail.Term.Name == m_termName).SingleOrDefault();
}
}
catch (Exception ex)
{
// handle exception
}

// make sure we got at least one result
m_tagCount = (termDetail != null && termDetail.Term.Name == m_termName) ? termDetail.Count : 0;
}
}

Getting the Welcome Page for a SharePoint Publishing Site

I wish I had found this sooner, it could have saved me a lot of time:

private static bool IsWelcomePage(string url)
{
bool rslt = false;

using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
if (PublishingWeb.IsPublishingWeb(web))
{
PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);
string defaulturl = pweb.DefaultPage.Url;

if (defaulturl == new Uri(url).GetComponents(UriComponents.Path, UriFormat.SafeUnescaped))
{
rslt = true;
}
}
}
}

return rslt;
}

Properties for SharePoint 2010 Visual Web Parts

I seem to spend a lot of time looking up where to set various properties in Visual Web Part projects, so I'm recording what I have learned recently. Here's how to set the Category that the web part will appear in when adding it to a page:
<Property Name="Group" Value="Acme" />
This property is in the Elements.xml file for the web part. In this case, the Category will be Acme.

Here's a picture of some things that affect the appearance of this feature in SharePoint under Manage Features. Title and description should be fairly obvious. The interesting thing about this feature is that it has a dependency on another feature called Social Ribbon. I found the GUID by looking in the TEMPLATE\FEATURES folder in the 14 hive. In my case, I opened the Feature.xml file in the SocialRibbonControl folder and copied the Feature Id value.


I also wanted show an image in the feature gallery next to the feature. First, I right clicked my project name in VS Solution Explorer and selected Add / SharePoint "Images" mapped folder. I created a sub-folder directly in the Images folder and copied my image there. Finally, I edited the Feature1.Template.xml file and added the following attribute to the Feature element: ImageUrl="Neudesic\Neudesic.jpg".

Finally, here are some properties that I added to the .webpart file: to affect the display of the web part in the web part gallery and to set a couple of properties that take effect when the web part displays on a page:
<property name="Title" type="string">Acme Web Part</property>
<property name="Description" type="string">Displays stuff, along with doing other stuff"</property>
<property name="ChromeType" type="chrometype">None</property>
<property name="Width" type="unit">200</property>

Tuesday, July 26, 2011

Error Handling Code for Web Part

I keep needing to write the same error handling code for Web Parts, HTML pages, etc. The only difference is in how I display the error. This is something I wrote for a client who told me that he wanted the web part to look ugly if an exception is thrown. Well, this does just that! The label with ID lblError is invisible until DisplayException gets called.
private void DisplayException(Exception ex)
{
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{

// show exception message
HtmlWriteLine("Exception", ex.Message, htmlTextWriter);
HtmlWriteLine("Method", ex.TargetSite, htmlTextWriter);
HtmlWriteLineReplaceNewLines("Stack Trace", ex.StackTrace, htmlTextWriter);

Exception exInner = ex.InnerException;
int innerExceptionNum = 1;
while (exInner != null)
{
htmlTextWriter.WriteBreak();
htmlTextWriter.Write("-----");
htmlTextWriter.WriteBreak();

HtmlWriteLine(String.Format("Inner Exception {0}", innerExceptionNum), exInner.Message, htmlTextWriter);
HtmlWriteLine("Method", exInner.TargetSite, htmlTextWriter);
HtmlWriteLineReplaceNewLines("Stack Trace", exInner.StackTrace, htmlTextWriter);

exInner = exInner.InnerException;
innerExceptionNum++;

htmlTextWriter.WriteBreak();
}
lblError.Text = stringWriter.ToString();
lblError.Visible = true;
}
}
}

private void HtmlWriteLineReplaceNewLines(string label, string text, HtmlTextWriter htmlTextWriter)
{
htmlTextWriter.Write(String.Format("{0}=", label));
string[] textArray = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string s in textArray)
{
htmlTextWriter.Write(s);
htmlTextWriter.WriteBreak();
}
}

private static void HtmlWriteLine(string label, Object value, HtmlTextWriter htmlTextWriter)
{
htmlTextWriter.Write(String.Format("{0}=\"{1}\"", label, value));
htmlTextWriter.WriteBreak();
}