I’ve been working on a utility for the past few days to manipulate some data on a SharePoint server. This code got the “works on my machine” stamp of approval, but every time we ran it against a backup of production data it would hang after processing a certain number of pages with no errors to be found anywhere. Since this was a backup of production data I tried deleting the file it was hanging on to see if that specific file was the issue…no luck. After a few hours of trying to track down the problem I eventually switched from using SPWeb.GetFile to SPWeb.GetFileOrFolderObject. The only impact this had on my code was I then had to cast the object type returned from GetFileOrFolderObject to SPFile. Magically the hang disappeared. I’m throwing this one in the SharePoint believe it or not bucket and moving on, in case you run into the same issue I’ve shown the difference in code below.
Here is the original code
1: SPFile article = web.GetFile(articleUrl);
2: articleGuid = article.UniqueId;
And the version that worked
1: object spObject = web.GetFileOrFolderObject(articleUrl);
2: articleGuid = ((SPFile)spObject).UniqueId;
I’ve shortened these for clarity, obviously you should be checking if you actually got a SPFile object back, but you get the idea.