How to properly use SPWeb.AllowUnsafeUpdates?

SharePoint2013

At times you may need to allow unsafe updates to SPWeb objects to get your code to run. This, in SharePoint C# code-behind is done by setting SPWeb.AllowUnsafeUpdates to true. However, as this is an exception to security settings, and should generally not be done, it’s a good practice to limit the change to as small a scope as possible – even though the setting is only persisted for the duration of the request (unless the SPWeb object was gotten from SPSite.GetWeb() or SPSite.Webs[]).

What to do?

I’ve found the easiest way to temporarily allow unsafe updates in a safe way but without too much of extra code to be this:

Please note, that it’s unwise to simply set the AllowUnsafeUpdates to false after the code, because there’s an ever-so-slight possibility of it screwing up some other code running in the same context at the same time! And of course, it’s likely to be unwise to allow unsafe updates if you’re handling data that was gotten as user input.

“File not found”-error when trying to activate custom feature on SharePoint

File not found

This post is about fixing on of the most non-spesific errors you can get when activating features on SharePoint “File not found”.

Problem

“File not found” is one of the more annoying errors one can stumble upon when trying to activate a feature.

File not found

File not found

There are quite a few reasons that can cause this error, but after the obvious ones (some files failing to be included in the package, botched wsp deployment, errorenous urls in event receiver) one thing to check might be the alternate access mappings. Continue reading

Attaching the Visual Studio debugger to the right IIS worker process

Visual Studio's Attach to Process -dialog

Every now and then comes the time when you’d actually need to debug something, and then you’ll likely be using your Visual Studio to attach the debugger to one or more processes.

Using CKSDev to attach to all IIS worker processes (w3wp.exe), or if you don’t use CKSDev, just pressing ctrl+p and selecting the processes from the list, is often a good enough solution. However, sometimes that makes your dev box sluggish, or maybe catches exceptions from code you’re not wishing to debug at the time, and it’d be handier to just attach to the one process you actually need. But how to find out the right one? Chances are, you’ll be having 3-6 w3wp processes, and you can only deduce so much from the process id…

Solution

Continue reading

Programmatically creating SPFields with readable internal names

SharePoint Field Name Problems

This post is about a small programmatic workaround to creating new SPFields for SPLists in SharePoint with human-readable internal names. This is mainly an usability improvement for your editors, but they’ll probably appreciate it!

Problem: non-readable internal names for SharePoint list fields

When you create a new field on SharePoint, SharePoint accepts the following syntax:

However, this will result in the internal name to be something like “Field_x0020_name_x0020__x002d__x” which is hardly readable, but rather quite horrible to look at, or use anywhere at all.

Luckily, there’s a nice and straightforward way to solve this issue and produce nice and readable internal names for SharePoint list fields!

Solution

If you need to get readable internal names (like when your editors actually use the internal names for something) for your fields, you can use a code similar to this:

Continue reading