Friday, December 6, 2013

How to fix the apostrophe issue in the JQuery AutoComplete control

I recently experienced an issue in which the JQuery AutoComplete control was throwing exceptions when attempting to locate an employee whose last name contained an apostrophe (i.e. O'Brien).  To resolve the issue, I simply added a line of code that replaced the apostrophe character with the proper escape sequence like so:

request.term = request.term.replace("'", "\\'");

Here is the complete function with the new line of code added in the appropriate location:

$("#<%=txtSelectUserName.ClientID %>").autocomplete({
    source: function (request, response) {
        request.term = request.term.replace("'", "\\'");
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/ADGroupManagementWS.asmx/GetAllADUsers",
            data: "{'keywordStartsWith':'" + request.term + "'}",
            dataType: "json",
            async: true,
            success: function (data) {
                response(data.d);
            },
            error: function (result) {
                alert("Due to unexpected errors, data could not be loaded");
            }
        });
    },
    minLength: 1
});

Monday, November 4, 2013

Programmatically call a Javascript function from an anchor tag's onclick event

While doing some work on a custom, navigation-like control on one of my SharePoint 2013 sites, I ran into a situation in which I needed to override the functionality of an anchor tag (i.e. <A>) so that it would call a javascript method and pass the URL as a parameter as opposed to simply directing the user to the referenced web page when the user clicks on it. In case you're curious, I need to do this because I'm going to display the dashboard/report that I'm referencing in a DIV tag as opposed to directing the user to the actual dashboard/report.  The following code sample provides a somewhat simplified example of what I did so that I can convey the concept without getting bogged down in too much detail (I included some of the Tableau dashboard code as a bit of a bonus as well as convey what I was up to with this):


<div id='menu'>
    <a href='http://UrlOfTableauReport1'></a>
    <a href='http://UrlOfTableauReport2'></a>
    ...
</div>

<div id='dashboard'></div>

<script>
     // Override the onlick so that it builds the dashboard in the DIV as opposed to redirecting the user to the dashboard's page

     jQuery( 'div a' ).click(function() {
          renderDashboard( this.href );
          return false;
     });

     // Construct the HTML associated with the Tableau dashboard that I'm going to display in a DIV tag 
     function renderDashboard(url) {
 var dashboardHTML = "<iframe width='700px' height='500px' class='tableauViz' src='" + url + "?:embed=y&amp;:host_url=https%3A%2F%2FTableauServerHostUrl%2F&amp;:tabs=no&amp;:toolbar=yes&amp;:allowscriptaccess=always&amp;:loadOrderID=5' frameborder='0' marginwidth='0' marginheight='0' allowtransparency='allowtransparency' style='border-bottom: medium none; border-left: medium none; padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 700px; padding-right: 0px; display: block; height: 500px; border-top: medium none; border-right: medium none; padding-top: 0px'></iframe>";
 document.getElementById("renderDashboard").innerHTML = dashboardHTML;
     }
</script>

Tuesday, October 8, 2013

How to define the CSS class that should be used during an onMouseOver or onMouseOut event for a web control

In case you would ever like to leverage an existing CSS class for your web control whenever an mouse event is triggered (i.e. onMouseOver or onMouseOut), here is a quick example of how to do that:

<input type="button" class="ui-state-default ui-widget-content" onMouseOver="this.className='ui-state-default ui-widget-content ui-state-hover'" onMouseOut="this.className='ui-state-default ui-widget-content'" value="Submit" id="btnSubmit" onClick="SubmitForm();" />

NOTE: In the above example, I'm leveraging the CSS classes that are already defined for a jQuery UI Calendar control that exists on another part of the page.  By leveraging those CSS classes for my control, I can keep the look and feel of all controls on the page as consistent as possible.

Also, there may come a time when you need to assign the CSS classes a web control uses via Javascript code. In the following example, I'm using a Javascript function to specify the CSS classes that will be programmatically defined for the given control based on the isSelected value (i.e. whether or not the control has been selected):

function SetButtonStyle(controlId, isSelected) {
     if (isSelected) {
          document.getElementById(controlId).onmouseout = "this.className='ui-state-default ui-state-highlight ui-state-active'";
          document.getElementById(controlId).onmouseover = "this.className='ui-state-default ui-state-highlight ui-state-active'";
          document.getElementById(controlId).className = "ui-state-default ui-state-highlight ui-state-active";
      }
      else {
          document.getElementById(controlId).onmouseout = "this.className='ui-state-default ui-widget-content'";
          document.getElementById(controlId).onmouseover = "this.className='ui-state-default ui-widget-content ui-state-hover'";
          document.getElementById(controlId).className = "ui-state-default ui-widget-content";
      }
}

Wednesday, September 18, 2013

View the raw XML returned from a REST call using Internet Explorer

If you are seeing the "user-friendly", feed reading view when calling a REST service via Inter Explorer and would prefer to view the raw XML for development purposes, here are the steps you can take to make that happen:
  1. Open Internet Explorer and enter your REST service call in the address bar
  2. Confirm that you're returned a view similar to the following:


  3. Click on Tools and select Internet Options
  4. Click on the Content tab
  5. Under the Feeds and Web Slices section, click on Settings
  6. Uncheck the checkbox for the item that reads "Turn on feed reading view"


  7. Click OK
  8. Click OK
  9. Refresh the browser and you will now see the raw XML returned from the REST service call


Thursday, September 12, 2013

Exception from HRESULT: 0x80131904

Over the years, I've periodically encountered an issue in which SharePoint will give you the following error message whenever you attempt to perform some simple write, update, or delete operations on a site (i.e. like adding a new list item to a SharePoint list):

Exception from HRESULT: 0x80131904

In my experience, this error message is indicating that SharePoint is not able to write information to the content database and the most common cause that I've encountered is that the location where SQL logs are stored has run out of drive space (i.e. this being the SQL server that hosts the SharePoint content database).  Typically, we've either deleted the older SQL transaction logs stored here or simply moved them to another location with more drive space (i.e. archive) in order to free up space on that drive location.  Once that delete/move operation of the log files is complete and more drive space is available, the SharePoint environment will return to normal operation immediately.

Friday, August 16, 2013

Convert web addresses contained in a string to hyperlinks using a regular expression

In the event you ever need to convert web addresses contained within a string to hyperlinks, I stumbled across some code on StackOverflow that will allow you to do this via a regular expression.  The code for this solution is presented as follows:


private string ConvertUrlsToLinks(string msg) 
{
    string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
    return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www");
}

Special thanks goes to a user by the name of "Rob" on StackOverflow for providing this solution to the problem!

Tuesday, August 13, 2013

Using C# to read data from a SharePoint list using the SharePoint REST API

If you're working with a C# application that is required to read information contained in a SharePoint list located on an external SharePoint farm, the SharePoint REST API can provide just the solution that you're looking for.  Here is some sample code that you can use for accessing the information contained in that SharePoint list:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;

namespace Sample
{
    public class SharePointListReader
    {
        ...

        public List<SharePointListItem> GetAllSPListItems()
        {
            List<SharePointListItem> posts = new List<SharePointListItem>();
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://webapp/site/_api/web/lists/getbytitle('listName')/items?$select=id,Title");
            request.Method = "GET";
            request.Accept = "application/json;odata=verbose";
            request.ContentType = "application/json;odata=verbose";
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Data data = null;

            // Read the returned posts into an object that can be consumed by the calling application
            using (response)
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    try
                    {
                        string jSON = reader.ReadToEnd();
                        data = serializer.Deserialize(jSON);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("An error occurred when reading the list items from SharePoint: {0}; {1}", ex.Message, ex.StackTrace));
                    }
                }
            }
            foreach (SharePointListItem post in data.d.results)
            {
                posts.Add(post);
            }
            return posts;
        }
    }

    public class Data
    {
        public Results d { get; set; }
    }

    public class Results
    {
        public SharePointListItem[] results { get; set; }
    }

    public class SharePointListItem
    {
        public string id { get; set; }
        public string Title { get; set; }
    }
}

Wednesday, August 7, 2013

Visual Studio hotkey for formatting XML: Ctrl+k+f

There will inevitably come a time when you're going to obtain some XML output that you need to review that would, in all honesty, be impossible to read without proper formatting.  A good example would be this response from the the YouTube API that provides a list of videos that a specified user has uploaded (i.e. which would appear as follows in Notepad):




In many cases, simply copying and pasting the XML it into Visual Studio might work, but, in the off chance it doesn't, you might get something like this.




If that happens, simply hit Ctrl+k+f and the XML will be displayed in an easy-to-read format:



Tuesday, August 6, 2013

Calculating the number of days since an event took place using Javascript

If you're looking for a way to determine how many days have passed since a given event took place using Javascript, here is a function that will provide you with the basic functionality that you're looking for:

function calculateDaysSinceEvent(eventDateTime) {
    // Obtain the current date and time
    var todaysDateTime = new Date();
    // Calculate the difference in time between today and when the event took place
    var diff = todaysDateTime - eventDateTime;
    // Convert the difference to a value that represents a day and round the value up
    return Math.round(diff/(1000*60*60*24));
}

Wednesday, July 31, 2013

Javascript function for converting a datetime object to a specific format

Chances are very good that at some point during your journey with SharePoint 2013 you're going to need a javascript function that you can use for converting a datetime object into a nice, user-friendly output string for display purposes.  In the code sample I'm about to provide, the javascript function will return an output string that matches the following format:

Wednesday, 7/13/2013

Here's the basic function that will give you the output presented above:

function getDateString(dateTime) {
 var weekday=new Array();
 weekday[0]="Sunday";
 weekday[1]="Monday";
 weekday[2]="Tuesday";
 weekday[3]="Wednesday";
 weekday[4]="Thursday";
 weekday[5]="Friday";
 weekday[6]="Saturday";

 var dayOfWeek = weekday[dateTime.getDay()];
 var month = dateTime.getMonth() + 1;
 var day = dateTime.getDate();
 var year = dateTime.getFullYear();

 return dayOfWeek + ', ' + month + '/' + day + '/' + year;
}


Saturday, July 27, 2013

Good solution for evaluating your web applications on various mobile devices - Ripple Emulator

If you're looking for a quick and easy solution for being able to evaluate the look and feel of your web applications or SharePoint sites across various mobile devices, the Ripple Emulator add-on for Chrome (created by the folks at tinyHippos) is definitely a great tool to have in your toolbox.  Once you activate/enable the add-on against a targeted page of a site, it will allow you to quickly select a target device and instantly view the layout of your page as it would appear on that device.

For SharePoint 2013 device channels, one important note is that the user agent being posted by the emulator doesn't match the device (at least at the time of this writing); therefore, you'll want to specify the channel to use by appending the following line at the end of URL in your browser's address bar:

?DeviceChannel=DeviceUserAgent

An example would be as follows:

http://SPWebApplication/Sites/Site/default.aspx?DeviceChannel=iPhone

Tuesday, July 2, 2013

Obtain all files contained under folders and subfolders within a SharePoint Document Library

If you are looking for a way to programmatically return all documents of a specific content type located within a given SharePoint Document Library regardless of whether they are contained in folders and subfolders within the given library, I've provided the following sample method that will allow you to do just that:

private SPListItemCollection GetDocumentsByContentType(SPList docLibrary, string contentTypeName)
{
    SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"RecursiveAll\"";
    SPListItemCollection documents = null;
    try
    {
        query.Query = string.Format("<where><eq><fieldref name="ContentType"><value type="Text">{0}</value></fieldref></eq></where>", contentTypeName);
        documents = docLibrary.GetItems(query);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return documents;
}

The key to this code being able to return all documents, including documents contained under folders and subfolders, is that I've set the ViewAttributes property to be Scope="RecursiveAll".  Without this particular setting, the query would have only returned items that were located immediately under the SharePoint Document Library and would have skipped any documents contained under folders and subfolders.

Tuesday, May 14, 2013

How to prevent an I-Phone App that keeps Location Services enabled from draining your I-Phone's battery

Occasionally, I've run into a few I-Phone apps that have a tendency to drain your I-Phone's battery to a nearly useless state in just a few hours. Most often, this is due to the fact that the app always has Location Services (i.e. GPS) running in the background whether or not you're using the app at the moment. Here are a few quick steps you can follow to save the life of your I-Phone's battery when you're not using the offending app:

  1. Go to your I-Phone's Home Screen
  2. Touch the Settings icon
  3. Scroll down and then touch the General icon
  4. Scroll down and then touch the Restrictions item
  5. At this time, you'll be prompted to either (1) enter your Restrictions passcode if you've already enabled Restrictions or (2) set up a Restrictions passcode (if you haven't set one up previously, you will need to do so now)
  6. Enter your 4-digit Restrictions passcode
  7. Scroll down to the Privacy section and touch the Location Services item
  8. Scroll down to the  item that represents the offending app and turn the value from 'ON' to 'OFF'
Once you're ready to begin your walk, run, or bike ride, simply follow these steps to re-enable the Location Services for the app:
  1. Go to your I-Phone's Home Screen
  2. Touch the Settings icon
  3. Scroll down and then touch the General icon
  4. Scroll down and then touch the Restrictions item
  5. Enter your 4-digit Restrictions passcode
  6. Scroll down to the Privacy section and touch the Location Services item
  7. Scroll down to the item that represents the offending app and turn the value from 'OFF' to 'ON'
Once Location Services are re-enabled for this app, it's ready to go.

With that said, the good news is that most app developers will resolve this issue once they're made aware of the situation; therefore, be sure to give them a heads up that you're encountering the problem and you're likely to receive a fix over the next few releases.  Anyway, this will provide an interim solution if you have the patience to toggle Location Services for the offending app.

Friday, April 12, 2013

Design Manager deployed Page Layout not displaying custom css file

I recently ran into an issue in which I was attempting to reference a custom css page within a Design Manager deployed page layout; however, I couldn't get the custom css page to load on web pages despite the fact that I was prominently referencing the css file in the proper location of the page layout's HTML file.  After doing a bit of research, I finally discovered that you also need to add a special attribute to the <link> element called "ms-design-css-conversion" and set the value to "no".  Here is an example of what your <link> element will look like in order for the custom css file to load properly:
<link href="MyCustomPageLayoutStyle.css" type="text/css" rel="stylesheet" ms-design-css-conversion="no" />
Also, here is a complete example that also shows you the proper location for referencing you custom css file within your custom page layout's HTML file:

<!--MS:<asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server">-->
<!--CS: Start Edit Mode Panel Snippet-->
    <!--SPM:<%@Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>-->
    <!--SPM:<%@Register Tagprefix="Publishing" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>-->
        <!--MS:<Publishing:EditModePanel runat="server" id="editmodestyles">-->
        <!--MS:<SharePoint:CssRegistration name="&#60;% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/editmode15.css %&#62;" After="&#60;% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/pagelayouts15.css %&#62;" runat="server">-->
        <!--ME:</SharePoint:CssRegistration>-->
    <!--ME:</Publishing:EditModePanel>-->
    <!--CE: End Edit Mode Panel Snippet-->

    <link href="MyCustomPageLayoutStyle.css" type="text/css" rel="stylesheet" ms-design-css-conversion="no" /> 

<!--ME:</asp:ContentPlaceHolder>-->

Wednesday, March 27, 2013

Programmatically determine if a web-scoped feature has been activated

This may be of limited use, but if you're ever looking for a way to programmatically determine if a web-scoped feature has been activated on a given SharePoint site, here is a quick method you can use to obtain that information:

private bool IsFeatureActivated(Guid siteId, Guid webId, Guid featureId)
{
    bool isFeatureActivated = false;
    using (SPSite site = new SPSite(siteId))
    {
        using (SPWeb web = site.OpenWeb(webId))
        {
            if (web.Features[featureId] != null)
            {
                isFeatureActivated = true;
            }
        }
    }
    return isFeatureActivated;
}

Monday, March 11, 2013

View the XML data behind a Search Core Results Web Part


If you wish to view the raw XML data that is associated with a Search Core Results or People Search Core Results web part, you can do so via the following steps:

  1. Open IE and navigate to the page that contains the given Core Results web part
  2. Select Site Actions -> Edit Page
  3. Click on your web part's menu and select Edit Web Part
  4. Expand Display Properties and click on the XSL Editor... button
  5. In the Text Editor window, copy the existing XSL to a temporary location since you'll probably need it again.  Here is a possible option for you:
    1. Hit Ctrl+a to highlight all of the content
    2. Hit Ctrl+c to save the content
    3. Open Notepad
    4. Hit Ctrl+v to past the content to Notepad
  6. Return to the Text Editor window, hit Ctrl+a, and hit Delete
  7. Copy and paste the following XSL code into the Text Editor window:
  8. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xmp><xsl:copy-of select="*"/></xmp> </xsl:template> </xsl:stylesheet>
  9. Click Save
  10. Click OK
  11. Return to the parent Search page and enter a valid search parameter
At this point, the page that contains the Search Core Results web part will be displayed with the XML contents available for your review.  Once your review of the data is complete, I recommend that you revisit the XLS Editor... and replace the XSL code with the code that you copied into Notepad earlier.  Unless, of course, your users like to view XML data in his/her search results which is not very likely...

Thursday, March 7, 2013

Assign a Preview image to your custom Page Layouts

There are quite a few good sites out there that provide steps for creating custom page layouts using the new SharePoint 2013 Design Manager, but none of them bring up how you would go about assigning a Preview image to your new custom Page Layouts. This is most likely due to the fact it's pretty simple to do once you know where to look for it. Anyway, these procedures assume that you've already created and deployed your new custom Page Layouts using Design Manager. Here goes...

Add the Preview Images to the Master Page Gallery
  1. Using your web browser, open up a link to your Master Page Gallery (i.e.  http://SharePointWebApp/_catalogs/masterpage/Forms/AllItems.aspx)
  2. Double-click on the en-us directory
  3. Double-click on the Preview Images directory
  4. Click on the Files tab
  5. Under the New section, click Upload Document
  6. Click Browse..., navigate to your first Preview file, and click on it
  7. Click Open
  8. Click OK
  9. For Content Type, select Master Page Preview
  10. For UI Version, place a check in the 15 checkbox



  11. Click Save
  12. Once the image is uploaded, click on the image list item and select Publish a Major Version
  13. Click OK
  14. Repeat Steps 5-13 for each Preview Image
NOTE:  If you don't publish your files as shown in Steps 12-13, your Preview images will appear as a red X when the user is looking at the locations where the Preview image is viewed


Assign the Preview Images
  1. Navigate back to the home page of the Master Page Gallery
  2. If you used the Design Manager to add your custom Page Layouts, you will click on the .html file associated with your first custom Page Layout and select Edit Properties (that is, if they are still associated with each other which is most likely the case...)
  3. Locate the Preview Image section
  4. Enter the URL of the file in the Type the Web address: textbox  (i.e. http://SharePointWebApp/_catalogs/masterpage/en-US/Preview%20Images/PreviewImageName.png)
  5. Enter a user-friendly description in the Type the description: textbox
  6. Click Save
  7. Repeat Steps 2-6 to assign the Preview images to their resepctive Page Layouts

Verify the Preview Image of a custom Page Layout
  1. Navigate to a page on your site and click Edit
  2. Click on the Page tab
  3. Under Page Actions, click Page Layout
  4. Verify that your custom Preview image has been assigned (i.e. in my example, my custom Page Layout is TestLayout1 and it's based on the Article Page content type)

Tuesday, February 26, 2013

Programmatically assign a SharePoint user to a "Person or Group" column in a SharePoint list

Here is some sample code that demonstrates how you can assign a given SharePoint user to a "Person or Group" column or field (in this case, I have a custom "Person or Group" field called "Owner") contained within a SharePoint list:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

...

Guid siteId = new Guid("GuidOfSiteCollection");
Guid webId = new Guid("GuidOfSite");
Guid listId = new Guid("GuidOfList");
string itemTitle = "My Item's Title";
string userName = "Domain//UserName";

using (SPSite site = new SPSite(siteId))
{
    using (SPWeb web = site.OpenWeb(webId))
    {
        // Find all list items that match your search criteria
        SPQuery query = new SPQuery();
        query.Query = string.Format("{0}", itemTitle);
        SPListItemCollection items = web.Lists[listId].GetItems(query);
        // Obtain a reference to SPUser object associated with the specified user
        SPUser user = web.AllUsers[userName];
        // Assign the user to the custom "Owner" field of my SharePoint list items
        web.AllowUnsafeUpdates = true;
        foreach (SPListItem item in items)
        {
            if (user != null)
            {
                item["Owner"] = user;
                item.Update();
            } 
        }
        web.AllowUnsafeUpdates = false;
    }
}
As you can tell, this is a super-simplified version of some actual code that I've got working. Naturally, you will want to organize this code to better suit your needs (as well as add some exception handling), but the purpose of this example is to show you the basics of how it's done. Enjoy!

Monday, February 25, 2013

How to obtain a copy of the InfoPath template file associated with a SharePoint Form Library

Here are the steps you can use for obtaining a copy of the InfoPath template file (i.e. template.xsn) after it has been published to a SharePoint form library:

  1. Open IE and navigate to the SharePoint site where your forms library exists
  2. Click on Site Actions -> View All Site Content
  3. Under the Document Libraries section, locate and click on the link for your form library
  4. On the Library Tools tab, click on Library


  5. Locate the Connect & Export section and click on Open with Explorer


  6. When Windows Explorer pops up, click on the Forms directory


  7. Locate the "template.xsn" file


  8. Copy and paste the "template.xsn" file to a location on your local hard drive
You may now edit that local copy of the template.xsn file using InfoPath and publish the form back to the SharePoint form library (i.e. the path information was saved to the form when it was published).

Tuesday, February 19, 2013

Programmatically obtain a list of files contained in a directory

If you ever need to programmatically obtain information about files contained within a given directory/folder, I've provided some sample code from a Windows Form Application I created that will provide you with that capability.  In this particular instance, my Windows App allows the user to select the directory he/she wishes to review and then populates a Grid View control with information about the files contained in the directory (i.e. in this simple example, the name of the file and the date the file was last modified):

using System.Windows.Forms;
using System.Data;
using System.IO;

public partial class Form1 : Form
{
        private FolderBrowserDialog m_fbd;

        ...

        /// <summary>
        /// When the button is clicked, the user will be prompted to select the input file to open via the OpenFileDialog
        /// control.  Once the user has selected the input file, the information in the file will be read, parsed, and then
        /// displayed in the grid view control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReviewFiles_Click(object sender, EventArgs e)
        {
            if (m_fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FileName");
                dt.Columns.Add("LastModifiedDate");

                DirectoryInfo dir = new DirectoryInfo(m_fbd.SelectedPath);
                foreach (FileInfo file in dir.GetFiles("*.*"))
                {
                    DataRow row = dt.NewRow();
                    row["FileName"] = file.Name;
                    row["LastModifiedDate"] = file.LastWriteTime.ToString();
                    dt.Rows.Add(row);
                }
                dataGridView1.DataSource = dt;
            }
        }

If you would only like to display information about certain types of files, you can modify the parameter of the GetFiles method call appropriately.  In the following example, the code will only display information about  .pdf files contained in the selected directory:

foreach (FileInfo file in dir.GetFiles("*.pdf"))

Yes, I know...this example provides too much code to demonstrate how easy it is to programmatically obtain info about files in a given directory, but I thought it would be fun to add the additional code that could turn this into a nice utility if needed...

Thursday, January 31, 2013

List of "illegal" characters that cannot be included in a SharePoint file name

I've been working on a utility that our team is going to leverage for rapidly uploading a ton of existing Policy and Procedure documents from file shares into a SharePoint site (by the way, this includes defining the relevant document metadata which is why we aren't simply copying the files over).  During my investigation, I happened to notice that many of the file names in the file share contain characters that SharePoint is not going to like at all (i.e. '&' is the leading offender).  In case you're curious what these "illegal" characters are for file names, here is a complete list:

  • Tilde
  • Number sign
  • Percent
  • Ampersand
  • Asterisk
  • Braces
  • Backslash
  • Colon
  • Angle brackets
  • Question mark
  • Slash
  • Pipe
  • Quotation mark

The source of this information is the following link: http://support.microsoft.com/kb/905231

If you're looking for a solution to this problem, one option is to use the code that I created in one of my previous posts that will allow you to rename the file as you're adding to to your document library:  Remove special characters from a string using Regular Expression


Thursday, January 24, 2013

How to find SharePoint 2013 Central Administration

I know that I'll be laughing about this post in the future, but I've got to be perfectly honest and tell you that it took me nearly 20 minutes to figure out how to access SharePoint 2013 Central Administration in my new Windows 2012 Server environment.  Here two different methods that I finally stumbled upon ways that will make this happen:

Method 1:
  1. Log on to your Windows 2012 Server environment
  2. Click on the center of the screen
  3. Move your mouse to the upper right corner of the screen and a panel will appear on the far right side of the screen
  4. Click on the Search icon

  5. Locate the Microsoft SharePoint 2013 section
  6. Click on the SharePoint 2013 Central Administration icon


Method 2:
  1. Log on to your Windows 2012 Server environment
  2. Click on the center of the screen
  3. Move your mouse to the upper right corner of the screen and a panel will appear on the far right side of the screen
  4. Click on the Start icon


  5. Move your mouse to the bottom of the screen and right click
  6. Click on the All Apps icon


  7. Locate the Microsoft SharePoint 2013 section
  8. Click on the SharePoint 2013 Central Administration icon
If it makes things easier, you can pin this icon to the Start page or Taskbar by replacing steps 6 and 8, respectively, as follows:
  1. Right-click on the SharePoint 2013 Central Administration icon 
  2. Click on the appropriate pin option icon

Tuesday, January 22, 2013

Programmatically obtain a list of SharePoint Web Applications located under a SharePoint Farm

If you ever need to programmatically obtain a list of web applications contained under your SharePoint Farm, here is some code that will do just that...

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPFarm farm = SPFarm.Local;
    SPWebService service = farm.Services.GetValue("");

    foreach (SPWebApplication webApp in service.WebApplications)
    {
        // The following code will need to be changed depending on your situation
        ComboBoxItem webAppItem = new ComboBoxItem(webApp.DisplayName, webApp.Id);
        cbWebApp.Items.Add(webAppItem);
    }
});

NOTE: In this particular instance, I'm writing my list of web applications to a combo box control of a Windows application. The idea is that this combo box control will be the first of several chained combo boxes that will allow you to dive deeper into the SharePoint objects based on the items selected in the combo box controls (i.e. Web Application -> Site Collection -> Site -> Document Library -> Document).  Perhaps this my inspire some ideas of how or why this code would be useful.

Tuesday, January 15, 2013

A solution for providing a "sum" operation on a calculated field in a SharePoint view

One of our site owners recently ran into a situation in which he was trying to create a SharePoint view that would provide a sum of all values contained within a calculated field, but was not able to do so via the simple configuration process.  By "simple configuration" process, I'm referring to the typical way that you would provide a sum on a standard number or currency field which would be to place your view in edit mode, scroll down to and then expand the Totals group, locate your field, select the "Sum" operation listed in the drop down control, and save the change to your view.  Unfortunately, this isn't possible for calculated fields due to the fact that calculated fields don't show up under the Totals group.  With this in mind, a few possible solutions that would provide the requested functionality are listed as follows:

  • Use a Content Query Web Part to host the XSLT code that would generate the desired view
  • Use SharePoint Designer to modify the XSLT code of the view directly.  In SharePoint 2007, you would do this by inserting a Data View web part and modifying the XLST of that web part. 
  • Change the calculated field to a regular number or currency field, add a workflow to the list that would perform the calculations previously associated with the calculated field, and place a sum operation on the field

After a discussion with the user about the requirements for this effort as well as the various options available, we came to the conclusion that it would be possible to use a simple, one-time solution to meet the needs of his effort.  In light of this, we chose the workflow option and decided to add our workflow to the list using SharePoint Designer.  If you're interested in this solution, here are the steps we took to make this happen:

Replace the calculated field with a standard number or currency field
  1. Using your browser, open the site and navigate to the list to be modified
  2. Under List Tools, click on the List tab
  3. Click on List Settings
  4. Under Columns, locate your calculated field and click on it's link
  5. Record the formula for your calculation
  6. Click Delete to remove the column
  7. Click on Create column
  8. For Column name:, enter the name of the original calculated field that you just deleted
  9. Select either the Number or Currency radio button depending on which you require
  10. Click OK
Create and publish the calculation workflow using SharePoint Designer
  1. Open Microsoft SharePoint Designer 2010
  2. Click Open Site
  3. For Site Name:, enter the URL of the site your list is contained under
  4. Click Open
  5. Under Navigation, click on Lists and Libraries
  6. Click on the list you wish to perform the calculations on
  7. Under Workflows, click on the New... icon
  8. Add an appropriate Title and Description for you workflow
  9. Click OK
  10. Once the Workflow Designer appears, click on the Action button
  11. Under Core Actions, select Do Calculation
  12. Click on the first value link
  13. Click on the function icon
  14. For Data source:, select Current Item
  15. For Field from source:, select your first field that is part of the calculation
  16. Click OK
  17. Click on the plus link and choose the appropriate option for your calculation
  18. Click on the second value link
  19. Click on the function icon
  20. For Data source:, select Current Item
  21. For Field from source:, select your second field that is part of the calculation
  22. Click OK
  23. Click on the Action button
  24. Scroll down to List Actions and click on Set Field in Current Item
  25. Click on the field link and select the field you just created previously that replaced the original, calculated field
  26. Click on the value link
  27. Click on the function icon
  28. For Data source:, select Workflow Variables and Parameters
  29. For Field from source:, select the Variable: calc item
  30. Click OK
  31. If needed, continue adding calculation steps until the workflow is complete
  32. Click the Save button
  33. Under Navigation, click on Workflows
  34. Click on the link associated with your newly created workflow
  35. Under Start Options, remove the check from the Allow this workflow to be manually started option
  36. Place a check in both the Start workflow automatically when an item is created and Start workflow automatically when an item is changed options
  37. Click the Save button
  38. Click the Publish button
Update your view to provide a sum on the new field
  1. Using your browser, open the site and navigate to the list to be modified
  2. Under List Tools, click on the List tab
  3. Click on List Settings
  4. Under Views, locate your view and click on it's link
  5. Scroll down to and then expand the Totals group
  6. Locate your new field and select the "Sum" operation listed in the drop down control
  7. Click Save
With these steps complete, the new "calculated" field will be updated by the workflow every time a list item is created or modified.  NOTE: Users will be able to enter values into the "calculated" field; however, the workflow will ultimately override this value when the items is created or saved.  At this point, you will now have a "sum" operation on your "calculated" field.