Changing Site Settings through code in SharePoint 2010
It’s been quite a while since I had the time to post. A customer dropped me this question today and I thought it’d be interesting to share.
He wanted to change the “Allow rules to specify another site as a target location”-setting (seen below) through code and needed to know if this could be done.
I hadn’t tried this before, but thought it’d be fun to try.
Finding the correct key
I suspected that the property could be found in Web.AllProperties, but I had no clue as to what the key might be called, so I simply iterated through all the keys twice. First with the setting enabled and secondly with the setting disabled. I wrote the keys to a label, and simply pasted them into Notepad so I could compare the two listings. The code I used to do this was quite simple:
foreach (String key in Web.AllProperties.Keys)
{ lblKeys.Text += key + "<BR>";
}
Having done this it was quite easy to identify the key I needed. It turned out it was called “_routerenablecrosssiterouting”. I recommend using the same approach if there is another setting you wish to change.
The final code
Now that I had the key name I was basically done. I used the following line to read the setting:
myString = (Web.AllProperties["_routerenablecrosssiterouting"]).ToString();
This is the code I used to change it:
Web.AllowUnsafeUpdates = true;
Web.AllProperties["_routerenablecrosssiterouting"] = "False";
Web.Update();
Since I was using a GET-request in my little sample I had to set AllowUnsafeUpdates = true
I’ve seen other people asking how to accomplish this on the net, so hopefully this post will be of use to you.
/ Johan
Comments
- Anonymous
February 17, 2012
Thanks Johan, its helpful.