Showing posts with label Sharepoint 2010. Show all posts
Showing posts with label Sharepoint 2010. Show all posts

Tuesday, March 27, 2012

Auto-closing SharePoint pop-up window after exporting to Excel

Recently I was working on a utility to export SharePoint list view data to a CSV file.. The logic was to open an application page using SharePoint's dialog framework, do the export logic in page-load of that application page and finally close the pop-up..

I thought closing the pop-up after completion would be fairly simple. I thought I can just use

Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");

after process completion. But here was the challenge..To save the CSV data, I had to use the following code:


HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.Write(csvData.ToString());
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
context.Response.Flush();
context.Response.End();



The problem was, after changing the content type of Response to "text/csv", it was not possible to execute any JavaScript from server code to close the pop-up window..

After trying many options, I was about to give up when I found this method definition on MSDN..
SP.UI.ModalDialog.commonModalDialogClose(dialogResult, returnVal);
Closes the most recently opened modal dialog with the specified dialog result and return value.
Now, the solution was simple.. All I had to do was to set a cookie in the server side after process completion, and in the parent page, run a timer job every few seconds in JavaScript to check for the same cookie, and if found, delete the cookie, stop the timer and close the pop-up..

Note: To process the cookie in JavaScript I used this great library, because of which it turned out to be just a single line of code..
http://code.google.com/p/cookies/

Here is the complete code..

Javascript to open and close the pop-up:

function OpenPopUp() {
    var oUrl =  'http://mysite/_layouts/SPExcelExport/SPExcelExport.aspx;
    var options = {
        title: 'Excel Export',
        allowMaximize: false,
        showClose: true,
        width: 300,
        height: 90
    };
    SP.UI.ModalDialog.commonModalDialogOpen(oUrl, options, null, null);//Open pop-up
    var millisecondsToWait = 2000; //Run every 2 seconds to check for cookie
    var intrvl = setInterval(function () {
        if (jaaulde.utils.cookies.get('csvexportcookie') != null) { //check if cookie is found
            jaaulde.utils.cookies.del('csvexportcookie'); //Delete the cookie
            SP.UI.ModalDialog.commonModalDialogClose(1, 1); //Close the pop-up
            clearInterval(intrvl);//Clear timer 
        }
    }, millisecondsToWait);
};

Server side code in the pop-up page:


HttpCookie oCookie = new HttpCookie("csvexportcookie", "completed"); //Set the Cookie
Response.Cookies.Add(oCookie);

HttpContext context = HttpContext.Current; //Write the CSV data to current response
context.Response.Clear();
context.Response.Write(csvData.ToString());
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
context.Response.Flush();
context.Response.End();

That was it.. Problem solved!

Wednesday, December 15, 2010

SharePoint 2010 installation on Windows 7 - Product Config Wizard fails in step 2 with System.FormatException

Today I was trying to install SharePoint 2010 enterprise on my Windows 7 laptop. The product configuration wizard was failing with the following error in Step 2 [creating configuration database].


The exception was:

System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).   at System.Guid..ctor(String g)   at Microsoft.SharePoint.Administration.SPFarm.GetInstalledProductIds()
Reason:
The following registry key had an "invalid" value "0" for the registry name {90140000-110D-0000-1000-0000000FF1CE} :
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server extensions\14.0\WSS\InstalledProducts
Solution:


This is the key that is used to determine the current edition (Foundation, Standard,Enterprise..) of SharePoint installed. The correct value of this should be:



88BED06D-8C6B-4E62-AB01-546D6005FE97 - For Enterprise trial
D5595F62-449B-4061-B0B2-0CBAD410BB51 - For Enterprise licensed
B2C0B444-3914-4ACB-A0B8-7CF50A8F7AA0 - For Standard trial
3FDFBCC8-B3E4-4482-91FA-122C6432805C - For Standard licensed

Changing this solved the problem.