Resolved: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

During my SharePoint project i need to clean up the site for better performance.  So first thing i started to delete the closed web parts from web part pages. As there were so many web part pages in the application so i decide to write a web application that can delete the closed web parts after run. Well when i run the application i got “The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.” error  on delete operations. I knew that this error was due to the fact that Web Page Security Validation was turned on that site and it was restricting me to delete the web part from my application. I can change the settings from Central Administration by turning off the security validation for that site but it’s not a good practices as any malicious code can harm your site as the security validation was off for that site. So i decide to handle this thing pro grammatically from my code so that it only disable security validation of web for a moment when only my code will run otherwise it will be on and protect my web from harmful activities. Well so here is the solution i applied.

Microsoft.SharePoint.Administration.SPWebApplication webApp = web.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;

// Insert your code for adding/deleting/updating here

webApp.FormDigestSettings.Enabled = true;

That’s it and i was back for deleting closed web parts 🙂

Advertisements

How to remove/delete SharePoint extended site placed in different zones

Well as we know that we can extend a SharePoint site for different zone other than default with different port number and host header. In extended site creation, a new site creates on IIS pointing to a new virtual directory on server. This new virtual directory has its own web.config file for extended site. But extended site points to same content db as the original site has. Therefore end user access same contents through different site urls depending upon the permission set on individual sites. In short you can say that extended site is a replica of its original site as both sites serve from same content db.

Now you want to remove/delete an extended web site from SharePoint server. What to do as extended site doesn’t show in “web application list” and in “Delete Web application”. Well no probes as I mentioned earlier that when we extend a web application it just create a site in IIS and point to existing site content database. So if we remove it from IIS and delete the virtual directory created by extended site then it should remove and delete from SharePoint server. But that’s a manually part and in this case we can miss some references. So here is the best practice for that:

  • Open Central Administration and go to Application Management tab.
  • Under “SharePoint Web Application Management” section, click on “Remove SharePoint from IIS Web site” link.
  • Select the actual web application from which you create extended site.
  • Under “Select IIS Web site and zone to remove” drop down list, expand the drop down by clicking the arrow button.
  • Here you will get all the extended sites with their zones including the actual website (Always created in Default Zone).
  • Select the extended site that you want to remove.
  • Under “Delete IIS Web sites”, check the Yes radio button and click OK.
  • Wait for processing and after completing the process you are done.

Reset Internet Information Services for SharePoint In Depth

When we create a new web application in SharePoint using central administration, we get a section of Reset Internet Information Services and by default its set to “Restart IIS Manually”. Many times we keep this option by default and create the application but even after successful web application creation we don’t reset IIS and go further for Site Collection creation without any problem. So what is the use of that option even we don’t do anything manually. So here is the explanation.

Single Server Scenario

When we create a new web application it asks for the Application Pool information to us. We can use an Application pool for new web application from the existing application pools or we can create a new application pool for the web application. Now if we create a new application pool then we need not to reset IIS as the purpose of IIS reset is just to recycle the application pool in which our newly created web application will reside. But if we use the existing application pool then it’s the best and recommended practice to reset IIS as may be of existing application pool will not recognize the new web application some time so we need to recycle this using an IIS reset.

Best Practice:

So for the best practices, always go for default option and do an “IISRESET /noforce” manually on application server whenever a new application created in a Single server scenario.

Farm Scenario

Another option i.e. “Restart IIS Automatically” never applies to application server (In which the CA exists) but this option is used to reset the IIS on other web front end servers reside in Farm. For the Application server either it is single server configuration or farm configuration you must reset IIS manually as it won’t reset IIS automatically for application server.

In the Farm scenario, If “Restart IIS Automatically” option is not selected and you have more than one server in the farm, you must wait until the IIS web site is created on all servers, and then run “IISRESET /noforce” on each Web server.  The new IIS site will not be usable until that is completed.

Best Practice:

So for the best practices, always select “Restart IIS Automatically” option and do an “IISRESET /noforce” manually on application server whenever a new application created in a FARM scenario.

Disable Language Selection of MOSS 2007 Site : Resolved

When we use variations with MOSS 2007 site, by default we get a drop down kind of control in the master page of site for language selection.

But there is a major disadvantage of this control I found while working with a publishing site.

Well the problem is if you are using some custom web part in your site and if these web parts work with query strings for their operation, then your default language selection control sucks.

Suppose you have a page URL like this “http://localhost/ENU/Pages/MyPage.aspx?FName=Kailash&LastName=Dhondiyal” and your page load the data according to your query strings (in my case this is first name and last name). Now your page loads properly in this scenario, well at this moment if you change the language of site to other language (in my case Hindi) then page post back and reload again with core URL i.e. without any query strings e.g. “http://localhost/HI/Pages/MyPage.aspx“. My page get crashed this simply means that when you change to another language you lost your all query strings data. For this purpose I create my own control for language selection so that on any language selection I can preserve my URL for proper functioning. Now I placed my custom language selection web part at proper place but I want to get rid of default language selection control so that user would use only my web part for language selection in place of default one. So below are the steps that how I disabled the default language selection control in my site.

1. Go inside the folder “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES”
2. Open “VariationsLabelMenu.ascx” file in visual studio.
3. Comment this line “<%–<cms:VariationsLabelEcbMenu id =”varlabelmenu1″ DataSourceID=”LabelMenuDataSource” DisplayText=”<%$Resources:cms,VariationLabelMenuTitle%>” IsCallbackMode=”true” runat=”server” />–%>”
4. Save changes and close the page.
5. Do an IIS reset?
6. Voilla your default language selection control gone.

Resolved: Remove Contextual Search Scope from Search Dropdown in SharePoint

By Default SharePoint search made up of two controls. First is a search drop down box that contains scopes.Second control is a serach box in which we type the keywords for search.

Scopes can be classified in two 2 categories.
1. Custom scope
2. Contextual scope

By default there are two custom scopes present in SharePoint:
1. All Sites
2. Peoples

In addition of these two scopes we can create our custom scopes according to our needs.

Well Contextual scope can be like this
1. This Site
2. This List

Depend upon the navigation the contextual scope changes.

* if we even remove the custom scopes it will always display atleast the contextual scope.

Well my article is here about how to get rid of contextual scope located in search dropdown box without any programming. In my scnerio i want a custom scope that search in a particular section of a site collection. We want this like we can search this site from any point of navigation. But after creating the custom scope and removing the default custom scope my search drop down was showing my custom scope with contextual scope i.e. This Site, that i din’t want to dispaly so that user could be restricted to only custom scope search. Well here what i did that.

1. Browse “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\OSearchEnhancedFeature” folder inside your SharePoint server.
2. Open SearchArea.xml file in notepad. (Please make a copy of SearchArea.xml before doing any modification.)
3. Add this property “<Property Name=”DropDownMode”>ShowDD_NoContextual</Property>” after this “<Property Name=”UseSiteDefaults”>true</Property>” property.
4. Save and close the file.
5. Do a IISRESET.
6. Browse your site and voilla now in search dropdown you have only your custom scope. Contextual scope This Site is gone…

Happy SharePointing 🙂

Resolved: COM DLL is not showing in COM Library after Registering on Windows Server 2008 x64

Well I had to implement a payment gateway at my server. My server was built on x64 architecture and had Windows Server 2008 installed. Well according to the document I had to put my COM dll inside c:\windows\system32 folder and then from Run command I had to register this dll like “regsvr32 c:\windows\system32\MyCOM.dll”.

Well I done above mentioned procedure successfully now I want to use this dll in my C# project so I create a project and go for add reference but even after successful registration my COM dll was not showing inside COM tab. I tried all possible way to add reference of this dll without any luck. I tried un-register, register as well as browsing this dll to add reference but nothing could help out me. So after doing some goggling and research I came to know that in x64 architecture there is a folder inside “c:\windows\” i.e. SysWOW64. This folder is specially designed for old 32 bit programs. Well if you are registering the old 32 bit COM dll or other 32 program then you should put the dll inside SysWOW64 folder and then register them from that location it will then use RegSvr32.exe of SysWOW64 folder for normal 64 bit dlls and other 64 bit programs u should put dlls inside System32 folder and run RegSvr32.exe from System32 folder.

CAML Query Search Between Two Date Ranges : SharePoint

SPSite site = new SPSite(“http://localhost:3001&#8221;);
SPWeb web = site.OpenWeb(“MyWeb”);
SPList list = web.Lists[“MyList”];

//RequestStartDate & RequestEndDate will be passed in mm/DD/yyyy format.
//They can extend  to work with time also.

string startDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(this.RequestStartDate)));
string endDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(this.RequestEndDate)));

SPQuery query=new SPQuery();
query.Query = “<Where><And><Geq><FieldRef Name=’Date_x0020_of_x0020_Meeting’ /><Value Type=’DateTime’>” + startDate + “</Value></Geq><Leq><FieldRef Name=’Date_x0020_of_x0020_Meeting’ /><Value Type=’DateTime’>” + endDate + “</Value></Leq></And></Where></Query>”;
DataTable dt = list.GetItems(query).GetDataTable();

This will return the records between two dates including time (both days will also include.). If you dont supply time it will take default time to search.

Get HTML of a web page using C#.NET

Below i am using a function that takes a web page full url as string and return back HTML of that page as string. You can
use this returned HTML in your code as per your requirement.

Step 1:
First import 2 namespaces that are required by this function to work.

using System.IO;
using System.Net;

Step 2:
Copy & Paste this function in your code behind.

public string GetHTML(string strURL)
{
HttpWebRequest wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method = “GET”;
HttpWebResponse wbrs = (HttpWebResponse)wbrq.GetResponse();
StreamReader sr = new StreamReader(wbrs.GetResponseStream());
strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}

Step 3:
Call this function as per your requirement.

string strHtml=this.GetHTML(“http://www.google.com&#8221;);

Happy Programming 🙂

File Download from SharePoint Document Library in Forms Based Authentication Scenario

I was writing the code for downloading a file from a SharePoint document library using Forms Based Authentication. Every time
file was getting downloaded with 8kb of size when i was opening them they were showing that file is not supported. I opened
one of downloaded files that was actually a word file, in notepad and i found some HTML code was inserting inside that file.
After looking the script i found that it was not actual file rather it was the login page of SharePoint site.. Humnn it means
there was some authentication problem while downloading and site was not authenticate the request for downloading the file
and hence request was redirecting to the login page.
I was using System.Net.WebClient and after some research i came to know that this class wouldn’t work directly in case of
Forms Based Authentication as it can’t persist the authentication credentials throughout the request.
I found that if i use HttpWebRequest, i can download the file but limitation of HttpWebRequest class is it can’t download a larger file.
Well i used the HttpWebRequest without any success.I was providing all proper credentials, method was executing without any exception and producing earlier result. It was driving me crazy. After hours of searching on net i couldn’t find any thing…
Well after reading some interesting things from different articles i decide to write my own class and method. So below is the
code by that i could achieve the expected result.

Step 1st:
Create a custom class inheriting from System.Net.WebClient. This class will take the object of CookieContainer class while
initializing, and it will override GetWebRequest method of System.Net.WebClient to change the default behavior of method
so that it can use of cookies and maintain the authentication credentials while making requests.

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class CustomWebClient : WebClient
{

private CookieContainer _cookies;
public CustomWebClient(CookieContainer cookies)
{
_cookies = cookies;
}

protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = _cookies;
return request;
}
}

Step 2nd:
Write a method to download the requested file. This method will actually call Authentication.asmx web service of SharePoint
site. Then it will take credentials and authenticate the request. After successful authentication it will create authenticated
cookie then we will add this cookie in a CookieContainer object and pass the object while initializing CustomWebClient object.

public void FBACookieAuthentication(string authenticationWSAddress, string userName, string password, string retVal, string docUrl)
{
try {
//retval=Download location on hard drive including document file name e.g. c:\MyFolder\SetUp.doc
//docUrl=Full url of the document needs to be downloaded e.g. http://loclahost/TEST/Error%20Resolution%20STSADM.txt
Authentication spAuthentication = new Authentication();
spAuthentication.Url = authenticationWSAddress;
spAuthentication.CookieContainer = new CookieContainer();
//Try to login to SharePoint site with Form based authentication
LoginResult loginResult = spAuthentication.Login(userName, password);
Cookie cookie = new Cookie();
//If login is successfull
if (loginResult.ErrorCode == LoginErrorCode.NoError) {
//Get the cookie collection from the authenticatin web service
CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
//Get the specific cookie which contains the security token
cookie = cookies[loginResult.CookieName];
//Initialize the cookie container of the list web service
CookieContainer cc = new CookieContainer();
cc.Add(cookie);
CustomWebClient httpRequest = new CustomWebClient(cc);
httpRequest.DownloadFile(docUrl, retVal);
}

} catch (SoapException ex) {
this.LogMessageToFile(“Exception Occured:” + ex.ToString());
} catch (Exception ex) {
this.LogMessageToFile(“Exception Occured:” + ex.ToString());
}
this.LogMessageToFile(“End FBACookieAuthentication method”);
}

That’s it………. Hope this blog entry will be useful to you……… Happy SharePointing 🙂

Resolved: Site Collection Administrator has limited permission to site.

Today i restored a moss site backup on a new port at my server. This site had Forms based authentication, after implementing
all the settings i logged in as a site collection administrator in to the site. When i click Site Actions links, i found some
options are missing in the menu e.g. “Edit Page”, “Create Page”, “Create Site”, “Site Settings” etc.
Though i was logged in as site collection administrator even then i was unable to found these options. So where was the problem?
After some research i came to know about “site quotas and locks” and i could fix the problems. Below are the steps:

  1. Open Central Administration
  2. Go to Application Management
  3. Under “SharePoint Site Management” section click on “Site collection quotas and locks” link
  4. Select desired “Site Collection
  5. Here in my case “Read-only (blocks additions, updates, and deletions)” was selected, so i select “Not locked” option and clicked on OK. All options were visible and working now.

🙂 🙂