Wednesday, March 14, 2012

Export the contents of a GridView to an Office 2007 Excel file

One of our testers recently asked me if there was a quick way to export the contents of a GridView control to an Office 2007 Excel output file.  After a bit of research, I was able to put together a functional solution that works, but is something that I'd be hesitant to use in a Production environment.  Perhaps this may inspire you to find a more optimal solution, but here is the quick solution that actually works:

First, you will need to add a button control to the aspx page on which the GridView is hosted as follows:

 <asp:Button runat="server" ID="btnExportToExcel" Text="Export to Excel"
        onclick="btnExportToExcel_Click" />

Next, you will need to add the following C# code to the aspx.cs file that is initiated by the button onclick event:

protected void btnExportToExcel_Click(object sender, EventArgs e)
{
    if (GridViewDisplay.Rows.Count > 0)
    {
        string outputFileName = "GetActivePropertiesByMarket.xls";
        // Prep the Response object to return the data as a file/attachment
        Response.Clear();
        Response.Charset = string.Empty;
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", outputFileName));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = outputFileName;
        // Obtain the data from the data source
        // NOTE: For the purposes of this example, I'm referencing a web method; however, you will probably use another means...
        ePM_New.YardiPropertySearchWS service = new ePM_New.YardiPropertySearchWS();
        DataSet ds = service.GetActivePropertiesByMarket(market);
        DataView dv = new DataView(ds.Tables[0]);
        // Bind the data to a second GridView that will be written to the output file...
        GridView grid = new GridView();
        grid.DataSource = dv;
        grid.DataBind();
        // Establish a means of writing the contents of the GridView to the output file
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(sw);
        grid.RenderControl(htmlWriter);
        Response.Write(sw.ToString());
        Response.End();
    }
}

Once you've updated your data source, you can fire up your web app and test the process of exporting the .xls document by clicking on the Export to Excel button. NOTE: That first IF statement you see in this code verifies that data is actually being displayed in the GridView that is exposed to the user on the web page.

Tuesday, March 13, 2012

Remove the "Add as Colleague" link from a My Site

Here is a simple trick that you can use to hide the "Add as Colleague" link on your My Site deployment in the event that you aren't quite ready to enable that functionality for your users.

  1. Open your browser and enter http://MySiteWebApp/person.aspx
  2. Click on Site Actions -> Edit Page
  3. Locate the Bottom Zone and click Add a Web Part
  4. Under Categories, select Media and Content
  5. Under Web Parts, select Content Editor
  6. Click the Add button
  7. Locate the newly added web part and click on the dropdown arrow in the upper right hand corner
  8. Select the Edit Web Part option
  9. Under Layout, place a check in the Hidden checkbox
  10. In the Content Editor web part, click on the link that reads Click here to add new content link
  11. In the Format Text ribbon, click on the HTML button and select Edit HTML Source
  12. In the HTML Source dialog, enter the following HTML:
    <style> .ms-my-addcolleague { DISPLAY: none }</style>
  13. Click OK
  14. Click OK
Ideally, the best solution is to add ".ms-my-addcolleague { DISPLAY: none }" to a your site's .css, but this is certainly a nice little trick to demonstrate the concept.

Monday, March 12, 2012

Automatically show "More Information" content on the Person.aspx page of a My Site implementation

By default, the content contained under the "More Information" section of the Person.aspx page (i.e. the information displayed in the ProfileDetailsViewer web part) is hidden and will only display when the user clicks on the More information hyperlink.  If you wish to display this content when the user accesses the page, here is one possible option you can use:
  1. Open your browser and enter http://MySiteWebApp/person.aspx
  2. Click on Site Actions -> Edit Page
  3. Locate the Bottom Zone and click Add a Web Part
  4. Under Categories, select Media and Content
  5. Under Web Parts, select Content Editor
  6. Click the Add button
  7. Locate the newly added web part and click on the dropdown arrow in the upper right hand corner
  8. Select the Edit Web Part option
  9. Under Layout, place a check in the Hidden checkbox
  10. In the Content Editor web part, click on the link that reads Click here to add new content link
  11. In the Format Text ribbon, click on the HTML button and select Edit HTML Source
  12. In the HTML Source dialog, enter the following HTML: ​
  13. <script type="text/javascript"> _spBodyOnLoadFunctionNames.push("ExpandMoreInformationSection");   function ExpandMoreInformationSection() { ShowHideDetails('ProfileViewer_ValueAboutMe', 'ProfileViewer_ProfileDetails', 'ProfileViewer_Ellipsis', 'ctl00_PlaceHolderMain_ctl11', 'More information', 'Hide information');}</script> 
  14. Click OK
  15. Click OK
From now on, when the user views a profile (including his/her own profile), the content contained in the the ProfileDetailsViewer web part will be shown on page load.  Naturally, it can be hidden again if the user clicks on the "Hide information" hyperlink.

Friday, March 9, 2012

Your solution file isn't being displayed in the Visual Studio 2010 Solution Explorer

In it's default state, Visual Studio 2010 isn't configured to display the solution in the Solution Explorer window.  If you wish to change the configuration to always display the solution, you can do so via the following steps:
  1. Open Visual Studio 2010
  2. Click on Tools -> Options
  3. Expand the Project and Solutions node
  4. Click on General
  5. Place a check in the Always Show Solution checkbox
  6. Click OK

Thursday, March 8, 2012

Obtain a list of users who haven't uploaded a picture in their SharePoint 2010 User Profile

In the event that your HR team would like to "bust" all individuals who haven't uploaded a picture in their user profile, here is a simple SQL query that you can run against the SharePoint 2010 User Profile content database that will provide them with a name and email address of the "offending" users:

SELECT
[UserProfile_Full].PreferredName,
[UserProfile_Full].Email
FROM [User Profile Service Application_ProfileDB].[dbo].[UserProfile_Full]
WHERE PictureUrl IS NULL

NOTE: You may need to modify the database name. If your SharePoint Admin used the default farm setup process, a GUID will be tagged to the database name.

Wednesday, March 7, 2012

Simple Redirect Web Part

If you need a quick solution for redirecting users from one SharePoint page to another, here is a very simple solution:

  1. Open your browser to the page of your SharePoint site which you'd like to place the redirect on
  2. Click on Site Actions -> Edit Page
  3. Locate the Bottom Zone and click Add a Web Part
  4. Under Categories, select Media and Content
  5. Under Web Parts, select Content Editor
  6. Click the Add button
  7. Locate the newly added web part and click on the dropdown arrow in the upper right hand corner
  8. Select the Edit Web Part option
  9. Under Layout, place a check in the Hidden checkbox
  10. In the Content Editor web part, click on the link that reads Click here to add new content link
  11. In the Format Text ribbon, click on the HTML button and select Edit HTML Source
  12. In the HTML Source dialog, enter the following HTML:
    <meta http-equiv="refresh" content="0;url=http://TargetSite/TargetPage.aspx">
  13. Click OK
  14. Click OK
When you no longer need the redirect web part, you can remove it from your page as follows:

  1. Open your browser, type in the address to the page of your SharePoint site on which you placed the redirect web part, and append "&contents=1" to it (i.e. http://SourceSite/SourcePage.aspx?contents=1)
  2. Place a check in the checkbox next to the redirect content editor web part you created above
  3. Click Delete to remove the redirect content editor web part