Tuesday, December 22, 2009

SPFile Item Update Error: Operation is not valid due to the current state of the object.

It's very frustrating and can take a whole working day: You are trying to update a library (list) item from Sharepoint object model and getting this error continiously.

In fact, solution is simple: RunWithElevatedPrivileges is working buggy in Sharepoint 2007, so the best things is avoiding putting code in it as much as possible.

For instance, if you want to simply update "Title" of an item in your document library, it should appear like this;

SPFile FileToModify = null;
SPSite ElevatedSite = null;
SPWeb ElevatedWeb = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite Site = new SPSite(SiteCollectionUrl))
{
ElevatedWeb = Site.RootWeb;
ElevatedSite = Site;
}
});

SPItem Items = FileToModify.Item;
ElevatedSite.RootWeb.AllowUnsafeUpdates = true;
Items["Title"] = "new title";
FileToModify.Item.UpdateOverwriteVersion();
ElevatedSite.RootWeb.AllowUnsafeUpdates = false;
Returning = "ok";

--

Then you won't face any errors.

Sunday, February 22, 2009

Silverlight: MultScaleImage Catches All Events On Page

As the version 2.5, Deep Zoom Composer has a good option for beginner coders to play with it's output's source code, including the both XAML and C# ones. These days many people choose to start developing nice photo shows with it's original output.

However, if you decide to add extra functionality such as buttons etc., you may face that MultiScaleImage (the Deep Zoom area) catches every click on whole page, even it is located in a seperate canvas apart from other elements.

This problem is caused by the original output generator. By default, all mouse events are binded to page. Just go to your source code and modify this.MouseLeftButtonUp etc. events into msi.MouseLeftButtonUp. After this, MultiScaleImage will only catch its mouse events, not the unrelated ones.

Monday, January 26, 2009

Visual Studio crashes in every Silverlight exception

It is very frustrating for even the smallest exception, Visual Studio 2008 crashes immediately. Some people are started to get used to live with that, however, there is a very very simple solution for it.

Probably you are using Visual Studio 2008 in another language like German, French etc. Just switch it to English from Visual Studio settings. No reinstallation needed.

Then - magic. Your Visual Studio will break operation when an exception thrown and you will be able to read the exception cause.

I am really unhappy with Silverlight guys in Microsoft to let these simple errors exist in Silverlight development. This is not a beta version, this is Silverlight 2's official release.