Sunday, January 25, 2009

Anyone know where I can find a good Locksmith?


I recently had the need to develop a WebPart for a client that needed to be able to "unlock" a site collection. For any of you that have tried this from within a WebPart running on the SharePoint platform, you know there are quite a few issues that can come up.

There are four different types of locks that can be set, here is a quick mapping of how those locks match up with the SPSite locking properties:

Not locked / nonesite.writeLocked = false
site.readOnly = false
site.readLocked = false
Adding content prevented / noadditionssite.writeLocked = true
site.readOnly = false
site.readLocked = false
Read-only / readonlysite.writeLocked = false
site.readOnly = true
site.readLocked = false
No access / noaccesssite.writeLocked = false
site.readOnly = false
site.readLocked = true

Additionally, when a site is locked using any one of the lock options, the site.LockIssue is used to describe the reason for the lock. If the site was locked using the Central Administraion console, the Additional lock information (LockIssue) field is required when locking the site. However, this parameter is not set when locking a site using the command line interface stsadm.

Here are just a few of the error messages I ran into while trying to set the lock options outright in my WebPart:
  1. Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.
  2. The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
  3. Access to this Web site has been blocked.
  4. Attempted to perform an unauthorized operation.
  5. Access denied.
With the exception of the first error, the remainder of the messages all center around permissions and access. With that in mind, I went about trying to build some solutions using the typical SharePoint methods to elevate the privileges of the code being executed. The first of these is the SPSecurity.RunWithElevatedPrivileges. With impersonation turned on in our web.config file, the code is running under the context of the SharePoint user. This method appears to elevate the code to run under the service account on the application pool.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
site.AllowUnsafeUpdates = true;
site.ReadLocked = false;
site.ReadOnly = false;
site.WriteLocked = false;
site.LockIssue = "";
}
});
Next, with no more luck, I tried to initialize a instance of the site class running under the context of my administration account.
using (SPSite site = new SPSite(siteUrl, SPContext.Current.Web.AllUsers["domain\\adminUsername"].UserToken))
{
site.AllowUnsafeUpdates = true;
site.ReadLocked = false;
site.ReadOnly = false;
site.WriteLocked = false;
site.LockIssue = "";
}
After a little bit more investigating, I discovered that in order to perform the unlock procedures that I needed, full blown code impersonation was needed. With a little bit of additional code, I had the access that I needed.
//get global admin access to perform the work
ImpersonateValidUser("domain", "adminUsername", "password");

using (SPSite site = new SPSite(siteUrl))
{
site.WebApplication.FormDigestSettings.Enabled = false;
site.AllowUnsafeUpdates = true;
site.ReadLocked = false;
site.ReadOnly = false;
site.WriteLocked = false;
site.LockIssue = "";
}

UndoImpersonation();
The code to implement the ImpersonateValidUser and UndoImpersonation functions can be found here: http://www.codeproject.com/KB/cs/zetaimpersonator.aspx, a wonderful bit of code from Uwe Keim.

By adding the line site.WebApplication.FormDigestSettings.Enabled = false to the code, we are able circumvent error #2. All of the remaining errors should be taken care of by our code impersonation. However, be warned that if the site has either readOnly or readLocked set to true, you will still have issues accessing values of various site properties. You will need to unlock the site to get the values that you need. If desired, you can then set the locks back to their previous state (assuming you have saved the state somewhere). And yes, you will even run into errors simply trying to save off this state for later use. The specific type of error that gets thrown will tell you everything you need to know about which of the three locks has been set.

When implementing the code, be sure that it is not executing when the page initially loads, otherwise you will run into error #1. Rather run the code during a page post back (i.e. in an event handler to manage the WebPart form submittal).

16 comments:

marilynndarevalo said...

Auto locksmith Indianapolis is centrally located and local for fast 24hr response. Car locksmith in Indianapolis is licensed and insured. indianapolis auto locksmith

jowdjbrown said...

If you locked your keys in the car, call Unlock Indy LLC at 317-590-2842. Our helpful car locksmith in Indianapolis will retrieve the keys quickly. calenderrdlocksmitharlington.com

Elizabeth J. Neal said...

Here are just a few of the error messages I ran into while trying to set the lock options outright in my WebPart: indianapolis mobile locksmith

Elizabeth J. Neal said...

Additionally, when a site is locked using any one of the lock options, the site.LockIssue is used to describe the reason for the lock. If the site was locked using the Central Administraion console, the Additional lock information (LockIssue) field is required when locking the site. However, this parameter is not set when locking a site using the command line interface stsadm. locksmith indianapolis

Elizabeth J. Neal said...

The Locksmiths Guild of Australia is a trade association which provides a range of services and support to its members. renolockservice.com

Yellow Blade said...

Công ty vận chuyển hàng hóa nội địa chúng tôi xin giới thiệu các dịch vụ vận chuyển, dịch vụ ship hàng uy tín để phục vụ nhu cầu Tết của quý khách hàng. Cụ thể chúng tôi sẽ cung cấp dịch vụ chuyển quà tết. Chúng tôi sẽ giúp bạn vận chuyển hàng hóa đến tay người thân, bạn bè ở xa một cách nhanh chóng nhất. Đảm bảo giá cả hợp lý chất lượng dịch vụ tuyệt vời. Ngoài ra chúng tôi còn cung cấp nhiều dịch vụ khác như dịch vụ ship hàng cod, giao hàng cho shop, dịch vụ chuyển phát nhanh trong nước,... Nếu cần chuyển hàng hãy nhớ liên hệ với chúng tôi nhé.

Joseph Russo said...

Furthermore, when a site is bolted utilizing any of the lock choices, the site.LockIssue is utilized to depict the purpose behind the lock. On the off chance that the site was bolted utilizing the Central Administraion support, the Additional lock data (LockIssue) field is required when bolting the site. Be that as it may, this parameter is not set when bolting a site utilizing the charge line interface..
locksmith in kirkland

Andrew Rogerson said...

Guys, sorry that it is off-topic, but I'm looking for a good locksmith. Urgently! Can someone help me with it? What can you tell about them? http://locksmithpalocksmith.com/ I really appreciate it!

Mark Meyers said...

Guys, do you know what is the price for lock replacing? I would like to compare prices before calling a locksmith http://locksmith-in-san-jose.com/

Richard C. Lambert said...

Here are just a few of the error messages I ran into while trying to set the lock options outright in my WebPart. flat rate lock n key las vegas

Maria Melo said...

If you answered no to any of these questions, it may prove useful to you to look up other locksmiths and compare their locksmith services, emergency locksmith services, security services, and general business history and reputation. Of all the people who you depend on, the person providing you with locksmith services should be among the most trusted. They see what you have inside your home or office, and they know how to get in quickly and easily. It wouldn't do to have your locksmiths services combined with his cat burglar skills to rip you off. 24 hour locksmith denver

Keep in mind that locksmith services are only as good and reliable as the locksmith performing those services. Make sure you find the best available locksmith for all your locksmith service, emergency locksmith service, or security service needs. Don't forget to check into the background of your locksmith carefully as well. I recommend that you always check the Better Business Bureau for history of complaints, but even better would be to use their website as a starting point for your search. Then you'll know that the locksmith services you're using are compliant with their standards, and can be trusted.

historypak said...

thank you for your interesting infomation. TONY"S LOCKSMITH BAY DR at COLLOQUIS

jowdjbrown said...

Personally I think overjoyed I discovered the blogs.
locksmith las vegas

jowdjbrown said...

It’s good to check this kind of website. I think I would so much from you.
Car Lockout Commerce City

Jon said...

To jowdjbrown

Check this interesting http://locksmith-allentown-pa.com/ it's helps you understand more about locksmiths history!

historypak said...

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. nationale slotenmaker antwerpen