I love my day job
December 2010 - Posts - SharePoint Skater

SharePoint Skater

Custom control and client script aficionado, neck-deep in a simmering SharePoint stew.

December 2010 - Posts

Phone number mask in a DataView WebPart (DVWP)

This came up today on the [sharepointdiscussions] list.  A user needed to display a read-only field in a phone number format; it's pretty simple, but it may be just what you need.

Assuming your list item contains a field called "Phone Number" (with a space), the following XPath will give you a number in the classic US telephone format:

<xsl:value-of select="concat('(',substring(@Phone_x0020_Number,1,3),')',substring(@Phone_x0020_Number,4,3),'-',substring(@Phone_x0020_Number,7,4))" />

If you need to mask an input, try this jQuery solution.

Generate a Word document from list data

This came up on a discussion list lately, so I threw together some code to meet the need.  In short, a colleague needed to take the results of an InfoPath form survey and give them to the user in Word format.  The form data was already in a list item, so it was a simple matter of using the SharePoint API to get the list item, formatting the data appropriately, and using response headers to make the client machine treat the response as MS Word content. 

The following rudimentary code can be run in an ASPX (or an assembly) in the 12 hive.  When you link to the page, send the list name and item ID in the querystring and use them to grab the appropriate data.

// Clear the current response headers and set them up to look like a word doc.
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset ="";
HttpContext.Current.Response.ContentType ="application/msword";
string strFileName = "ThatWordFileYouWanted"+ ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);

// Using the current site, get the List by name and then the Item by ID (from the URL).
string myListName = HttpContext.Current.Request.Querystring["listName"];
int myID = Convert.ToInt32(HttpContext.Current.Request.Querystring["itemID"]);
SPSite oSite = SPContext.Current.Site;
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["MyListName"];
SPListItem oListItem = oList.Items.GetItemById(myID);

// Build a string with the data -- format it with HTML if you like.
StringBuilder strHTMLContent = newStringBuilder();

// *
// Here's where you pull individual fields out of the list item.
// *

// Once everything is ready, spit it out to the client machine.

HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

More Posts