Tuesday, 22 April 2014

SharePoint 2010: Add an Event Receiver in code

Some requirements demand the adding of Event Receivers in code and not as features. Fortunately, there is a simple method to perform this task.

The following code adds an 'ItemAdding' event receiver in the class 'MyEventReceiver'. The project namespace is 'MyProject'

using (SPSite site = new SPSite("http://blah.com"))
{
   using (SPWeb web = site.OpenWeb())
   {
      SPList list = web.Lists["MyList"];
      list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "MyProject.MyEventReceiver");
   }
}




SharePoint 2010: How can I change my Date column to display only the date (DateOnly)?

I thought this would be a piece of cake - open up SharePoint Manager, get the XML and done. Unfortunately, that is not the case. The property DisplayFormat holds the value, and the XML produced seems fine but the field kept on displaying the time.

I then saw this article from Microsoft with the answer - the XML element is Format, NOT DISPLAYFORMAT.

Adding Format="DateOnly" to my field XML resolved the problem.

Tuesday, 15 April 2014

SharePoint 2010: How I can stop the timeout when I am debugging server side code in Powershell?

Debugging code can be frustrating at times - even more so when your attachment to the w3wp.exe times out. A simple solution to the problem is to disable the 'Ping' on the application pool. This is accessed through the 'Advanced Settings'.


Alternative, if you are lazy like me, you can run the following PowerShell script to disable it on all web applications:

Get-WmiObject -Class IISApplicationPoolSetting -Namespace "root\microsoftiisv2" | ForEach-Object { $_.IdleTimeout=0; $_.PingingEnabled=$false;  $_.Put(); }


Monday, 14 April 2014

SharePoint 2010: AllowEveryoneViewItems

A blog I received from Ishai Sagi's blog SharePoint Tips and Tricks highlighted a SPList property that I was not aware of: AllowEveryoneViewItems.

It seems that this property allows you to overwrite the item permissions for access to a document or list attachment, regardless of the other permissions. See more here.


Thursday, 10 April 2014

Windows Server 2008 R2: Group Policy changes for a better RDP and Development environment experience

My development VM is running off Windows Server 2008 R2. Some of the default settings are not conducive to easy development, so I made the following changes to make my life easier:

1.       Turn off User Access Control (see here)
2.       Make the following Group Policy changes (go to gpedit.msc > Computer Configuration > Adminstrative Templates> Windows Compopnents > Remote Desktop Services > Remote Desktop Session Host)
a.       > Session Time Limits > Set time limit for disconnected sessions  > Disable
b.      > Session Time Limits > Set time limit for active but idle Remote Desktop sessions  > Disable
c.       > Device & Resource Redirection > Do not allow clipboard > Disable
3.       Move page file to another drive if your running out of space on C: (see here)
4.  Remove the automatic log-off settings (see here)

Wednesday, 9 April 2014

SharePoint 2010: Why is my new inline style not being rendered through the SharePoint client object model?

My page has a DIV that needs to be hidden based on some runtime logic. I was running my code after the page loaded,

 SP.SOD.executeOrDelayUntilScriptLoaded(function () {
        hideMyStuff($data);
    }, "SP.js");

My function 'hideMyStuff' called another function to handle the hiding logic. My hiding code was simple:

function hideDIV(divId)
{
    document.getElementById(divId).style.display = 'none';
}

That should work, but nothing was happening. On further inspection, I noticed that the CSS class in the page was using a lot of '!important' with its CSS settings, which was overriding my code.

This left 2 options to resolve the problem:

1. Sledgehammer approach: set the value with !important.
The method would become
    document.getElementById(divId).style.display = 'none!important';

2. Stylish approach:  add a new class to the DIV at runtime.
The class is simple:
.hidden {
    display:none;
}

and the hide method (using JQuery) becomes:
 $("#" + divId).addClass('hidden');

Tuesday, 8 April 2014

SharePoint 2010: Error code 12031 in WCF Endpoint called from the Client Object Model

I created a simple WCF endpoint to reference a REST service to return data from the client object model. All was going well until a single test account started to fail. One would work, another would fail.

I found the answer here.

Once of the scenarios in the code was not setting a DateTime property in the DataContract class.

I changed the code to set the value of the field to DateTime.MaxValue to resolve the problem.

How do I enable my clipboard when I remote into a Windows Server 2008 R2 VM?

Not being able to cut and paste between a host PC and a VM is more than a little irritating. The problem is a group policy that, by default, disables this functionality. To enable it.

1. Edit the VMs Group Policy



2. Set the 'Do Not Allow Clipboard Redirection' to Disabled


3. Reboot the machine for the changes to take effect.

Thursday, 3 April 2014

How do I set my username and password for Mercurial (no more prompting please)?

My new project uses Mercurial as the source control. The daily pull of data becomes a little tedious when you are constantly prompted for credentials. I have add a few simple settings to resolve the (annoying) problem.

Open the workbench, right click on the repository and select settings. The name of the settings file is displayed at the top of the repository settings page.

My location was c:\MyProject\.hg\hgrc

Now, open this file in your favourite text editor and add a few simple entries:

[ui]
username=Richard Leeman <richard@leeman.com>

[auth]
repo.prefix=http://myrepositorylocation/code
repo.username=Richard Leeman
repo.password=P@$$w0rd

This is not the most secure way to store the credentials, but it certainly beats having to enter them every time.