Gino Abraham's Blog

March 2012 Entries

A Console Application or Windows Application in VS 2010 for Sharepoint 2010 : A common Error

I have seen many Sharepoint Newbies cracking their head to create a Console/Windows  application in VS2010 and make it talk to Sharepoint 2010 Server. I had the same problem when i started with Sharepoint in the begining.

It is important for you to acknowledge that SharePoint 2010 is based on .NET Framework version 3.5 and not version 4.0.

In VS 2010 when you create a Console/Windows application, Make Sure you select .Net Framework 3.5 in the New Project Dialog Window.If you have missed while creating new Project Go to the Application tab of project properties and verify that .NET Framework Version 3.5 is select as the Target Framework.

Now that you have selected the correct framework, will it work? Nope if the application is configured as x86 one it will not work. Sharepoint is a 64 Bit application and when you create a windows application to talk to Sharepoint it should also be a 64 Bit one. Go to Configuration Manager, Select x64. If x64 is not available select <New…> and in the New Solution Platform dialog box select x64 as the new platform copying settings from x86 and checking the Create new project platforms check box.

This is not applicable if you are making a console application to talk to sharepoint with Client Object Model.

Gettings Terms asscoiated to a Specific list item

I had a fancy requirement where i had to get all tags associated to a document set in a document library. The normal tag could webpart was not working when i add it to the document set home page, so planned a custom webpart.

Was checking in net to find a straight forward way to achieve this, but was not lucky enough to get something. Since i didnt get any samples in net, i looked into Microsoft.Sharerpoint.Portal.Webcontrols and found a solution.The socialdataframemanager control in 14Hive/Template/layouts/SocialDataFrame.aspx directed me to the solution. You can get the dll from ISAPI folder. Following Code snippet can get all Terms associated to the List Item given that you have list name and id for the list item.

using System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

Microsoft.SharePoint;

using

Microsoft.Office.Server.SocialData;

namespace

TagChecker

{

class Program

{

static void Main(string[] args)

{

// Your site url

string siteUrl = http://contoso;

// List Name

string listName = "DocumentLibrary1";

// List Item Id for which you want to get all terms

int listItemId = 35;

using (SPSite site = new SPSite(siteUrl))

{

using(SPWeb web = site.OpenWeb())

{

SPListItem listItem = web.Lists[listName].GetItemById(listItemId);

string url = string.Empty;

// Based on the list type the url would be formed. Code Sniffed from Micosoft dlls :)

if (listItem.ParentList.BaseType == SPBaseType.DocumentLibrary)

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.Url.TrimStart(new char[] { '/' });

}

else if (SPFileSystemObjectType.Folder == listItem.FileSystemObjectType)

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.Folder.Url.TrimStart(new char[] { '/' });

}

else

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url.TrimStart(new char[] { '/' }) + "?ID=" + listItem.ID.ToString();

}

 

SPServiceContext serviceContext = SPServiceContext.GetContext(site);

Uri uri = new Uri(url);

SocialTagManager mgr = new SocialTagManager(serviceContext);

SocialTerm[] terms = mgr.GetTerms(uri);

foreach (SocialTerm term in terms)

{

Console.WriteLine(term.Term.Labels[0].Value );

}

}

}

Console.Read();

}

}

}

Reference dlls added are Microsoft.Sharepoint , Microsoft.Sharepoint.Taxonomy, Microsoft.office.server, Microsoft.Office.Server.UserProfiles from ISAPI folder.

This logic can be used to make a custom tag cloud webpart by taking code from OOB tag cloud, so taht you can have you webpart anywhere in the site and still get Tags added to a specifc libdary/List.

Hope this helps some one.