Gino Abraham's Blog

SharpDevelop - Open Source IDE for C#/VB.Net Projects May 02

#develop (short for SharpDevelop) is a free IDE for C#, VB.NET and Boo projects on Microsoft's .NET platform. I found this usefull.

It is open-source, and you can download both sourcecode and executables from http://www.icsharpcode.net/OpenSource/SD/Download/

Configuration Data in a Custom Timer job in SharePoint 2010 : The Hierarchical Object Store Apr 05

I was planning for a custom timer job for which i wanted to store some configuration data. Was looking for some best practices, found a useful links on The Hierarchical Object store Store http://www.chaholl.com/archive/2011/01/30/the-skinny-on-sppersistedobject-and-the-hierarchical-object-store-in.aspx 

Initially was planning for a custom list, but this would make us run a cross site query and the list name and the url should again be kept in some configuration which is an headache to maintain. Hierarchical object store was zeroed in and thanks to Google for the same :)
 

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

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 Mar 22

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.

Attempted to perform an unauthorized operation – Error while updating other user’s profile data in Sharepoint 2010 Dec 14

We had a requirement where a user profile property will be updated from the event receiver of a custom list. The user profile property which we can had “user can override” and “Allow users to edit”, however we were getting an error “Attempted to perform an unauthorized operation”

The code was running under RunWithElevated delegate. We traced the user id which was Sharepoint\System. The system account in use will be the App pool Identity under which the current Sharepoint site is running. We took app pool identity id and gave full permission for user profile service application. This didn’t solve our problem.

Solution:

Get the App pool identity, of the Sharepoint site from IIS.

Go to Sharepoint Central Admin > Application Management > Manage Service Application.

Select the User Profile Service Application which is associated to your site, and select Manage Permission from top ribbon. Add the App Pool Id and give full control.

Then select Administrators in the TOP ribbon for User Profile service application and add the app pool identity id and give full control.

 

Block Application pages and Form Pages in Sharepoint 2010 Nov 03

By default Sharepoint doesn’t blocks user with limited access from visiting application pages (for ex _layouts/viewlsts.aspx).

Someone who knows the URL, can go to this page.

 We can avoid this by change the limited access to lockdown mode. Use the command below.

  

Action

Command

Turn on lockdown mode for a site collection

stsadm -o activatefeature -url <site collection url> -filename ViewFormPagesLockDown\feature.xml

Turn off lockdown mode for a site collection

stsadm -o deactivatefeature -url <site collection url> -filename ViewFormPagesLockDown\feature.xml

For more info on this visit http://technet.microsoft.com/en-us/library/cc263468(office.12).aspx#section6 

 

 Once locked down mode is enabled, groups/users with View Application pages will only be able to visit these pages. You can either select Restricted Read permission or remove View Application Pages permission for the users or groups which you want to block application pages.

ULS Tracelog To WSS_Logging Database in Sharepoint 2010 Jul 05

By Default ULS trace logs are logged to text file in 14 Hive\LOGS. In sharepoint 2010 we have a provision to move these logs to WSS_Logging database.  Follow these steps to get ULS tracelogs to WSS_Logging Database.

 

  1. Open the SharePoint Central Administration site

  2. Navigate to the Monitoring section

  3. Select Review Job Definitions

  4. Click on the job with the name Diagnostic Data Provider: Trace Log

  5. Click on the Enable button to enable it

  6. Click on Run Now, to start it immediately

     

 

Add Team Site Under Publishing Site as a Subsite May 20

By default when you try create a subsite under a Publishing site with will give you only 2 options. (Publishing Site or Enterprise Wiki).

But we can enable as many site templates as we want. Go to Site Actions > Site Settings > Look And Feel > Page Layout and Site template.

Select the list of templates you want.

 

Accessing Interop dlls makes winforms exes non responsive Apr 25

In one of my windows application, I was making use of Domino inerop for accessing document data from lotus notes. Whenever the interop was probed for some bigger response, my application use to be non responsive or hang.Once the call to inerop is done the application was accessible. Users will not know the progress in such scenarios.

You can avoid this by introducing a Background thead. Move all the inerop method calls to a seperate function. On you event handler add a background thread and call the function.

Thread newThread = new Thread(FunctionWithInteropCalls);

newThread.IsBackground = true;

newThread.Start();

If the thread is marked as a Background thread, the thread will be killed automatically during Application.Exit()

Now if function which is executed by newThread should access windows objects created in the main thread, you will have to handle it. Sample below shown how you can achieve this.

public void AccessMainThreadObj(string value)
{
if (InvokeRequired
{
this.Invoke(newAction<string>(AccessMainThreadObj), value); 
return;
}
if(value.StartsWith("#1"))
    textbox1.Text = value.Substring(2);
else
   richTextBox2.Text += value;
}
  
 
void FunctionWithInteropCalls()
{
AccessMainThreadObj("#1 To update textbox1");
AccessMainThreadObj("To update richTextBox2");
}
 

 Create a method to update the value of the controls in the form. Basically this function checks whether the current process needs an invoke, if yes comes through the builtin invoke method.

 

 

Enums in C# - To get text value from corresponding number Apr 14

Enums are always a favourite when we want to restrict users while sending some string to a generic methods. Was exploring how to get the text of the enum from the correspoing number. .Typecasting the number to the enum type would get the text from enum. Have shared a sample on this

Using System;

namespace ConsoleApplication1 {

class Program{

enum WeekDays { Sun = 1, Mon = 2, Tue, Wed, Thu, Fri, Sat }

staticvoid Main(string[] args) {

Console.WriteLine(((int)WeekDays.Thu).ToString());

Console.WriteLine(((WeekDays)1).ToString());

Console.Read();

}}}

Output :

5

Sun