Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.
Showing posts with label Errors in SharePoint. Show all posts
Showing posts with label Errors in SharePoint. Show all posts

Friday, December 02, 2011

Handling AccessDenied Exception to implement our logic

The SPUtility.HandleAccessDenied method provides the functionality to redirect users to the standard "Access Denied Page" pragmatically, thus asking them to re-logon. One of the scenarios of such usage is the public sites, where access to the standard SharePoint specific pages still exists and you want to block those pages
However, you can handle access denied exception via SPSecurity.CatchAccessDeniedException = true
Access deined exception is handled by sharepoint platform and user will be redirected to _layouts/AccessDenied.aspx page if user doesnt have permission to perform that task. This might casue usability problem in some cases. You can handle access denied exception in your code by setting CatchAccessDeniedException value to true.
Following code snippet shows how to handle access denied exception:

bool catchException = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
//updating list item
SPList list = SPcontext.Current.Web.List["TestList"];
SPListItem item = list.Items[0];
item["title"] = "Some value";
//If user doesnt have permission, exception will be thrown
//If value of CatchAccessDeniedException is true, then user will be
//redirected to AccessDenied.aspx page
item.Update();
}
cach(Exception ex)
{
//Your custom error message can be shown here
}
finally
{
//reset the flag to original value
SPSecurity.CatchAccessDeniedException = catchException;
}

Failed to Create the Configuration Database Error in SharePoint 2010

SharePoint Products Configuration Wizard
Configuration Failed
Failed to create the configuration database
An exception of type System.IO.FileNotFoundException was thrown. Additional exception information: Could not load file or assembly 'Microsoft.IdentityModel, version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.



Cause.

This error is not related to installing or configuring SQL Server 2008 for SharePoint 2010. This error is about the Geneva Framework.

Solution.

Please uninstall the Geneva Framework. Run SharePoint 2010 setup and click on "Install Software Prerequisites" to download and install the version of Geneva Framework needed by SharePoint 2010.

Thursday, December 01, 2011

How to fix Security Validation errors in Sharepoint asp.net page

How to fix Security Validation errors in Sharepoint asp.net page
 Problem: I got "The security validation for this page is invalid" when
submitting web form

For reasons of security, Microsoft Windows SharePoint Services by default
does not allow you to make posts from a Web application to modify the
contents of the database unless you include security validation on the page
making the request. Two kinds of security validation can be used, depending
on whether the code on the page applies globally to a virtual server or
Windows SharePoint Services deployment, or to a single site or site
collection within the deployment.

Security Validation Type 1:

Updating data for a site or site collection. Two steps to be performed.

Step 1:

Add a page directive and a FormDigest control to the page making the
request. The following directive registers the
Microsoft.SharePoint.WebControls namespace:

<%@ Register Tagprefix=”SharePoint”
Namespace=”Microsoft.SharePoint.WebControls”
Assembly=”Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c” %>

Step 2:

Include a FormDigest control within the form as follows:

<form id=”Form1″ method=”post” runat=”server”>
<SharePoint:FormDigest runat=”server”/>
<asp:Button id=”Button1″ style=”Z-INDEX: 101; LEFT: 282px; POSITION:
absolute;
TOP: 282px” runat=”server” Text=”Button”></asp:Button>
</form>

Inserting this control on an ASPX page generates a security validation, or
message digest, to help prevent the type of attack wherein a user is tricked
into posting data to the server without knowing it. The security validation
is specific to a user, site, and time period and expires after a
configurable amount of time. When the user requests a page, the server
returns the page with security validation inserted. When the user then
submits the form, the server verifies that the security validation has not
changed. For more information about this control, see the FormDigest class.
Security Validation Type 2:

Updating global data

Web applications that use methods of the Microsoft.SharePoint.Administration
namespace, such as for creating or deleting sites and for global
administrative customizations, require a different security validation. Add
the following code to the .vb r .cs file in an application:

SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
Context.Items[SPGlobalAdmin.RequestFromAdminPort] = true;
Page.RegisterHiddenField(”__REQUESTDIGEST”, globalAdmin.AdminFormDigest);

This security validation uses the AdminFormDigest property of the
SPGlobalAdmin class to insert a message digest on the page in the browser,
registering the digest as a hidden field through the RegisterHiddenField
method of the System.Web.UI.Page class. In addition, the
RequestFromAdminPort field specifies that the context of the request is
through the administrative port

Important Error we will get while working with features in SharePoint



Feature definition files must be named 'feature.xml'

Recently I was trying to manually install a custom MOSS feature (by running STSADM.EXE from the command line), but I simply couldn't get it to work. The error returned by STSADM was: "Invalid file name. Feature definition files must be named 'feature.xml'". Normally I deploy features using the BAT-files that are part of the workflow projects I'm creating, so this was a first for me...
I edited the contents of the feature.xml file, renamed it from Feature.xml to feature.xml, and made other changes as well. Finally I tried to explicitly specify the current directory so I modified the STSADM parameter to include ".\" and then a new error appeared.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\RegisterPolicyResource>STSADM.EXE -o installfeature -filename .\feature.xml -force
Failed to find the XML file at location '12\Template\Features\.\feature.xml'

I then realized that the problem was that STSADM wanted to have the filename given to it relative to the directory C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES, regardless of where you are located when you run STSADM. So the solution was to change the call to: STSADM.EXE -o installfeature -filename RegisterPolicyResource\feature.xml -force

So the problem wasn't really that the the definition file wasn't named feature.xml, but that I ran STSADM assuming that the feature.xml whould be relative to the directory I was running STSADM from.