..:: I like SharePoint ::.. Rotating Header Image

Visual Studio

SharePoint How to create ASPX Forms (#SPForms)

SharePoint offers a lot of options to create custom list forms like jQuery, SharePoint Designer, InfoPath (last Release) and of course ASPX forms. Regarding the last point i demonstrated a shorter “how to” at the Shareconf 2014 in Düsseldorf and would like to share the idea behind the ASPX Forms in this post.

The ASPX Forms (SPForms) which i mean are based on the use of the rendering templates along with ascx user controls.


The rendering template contains all elements which are needed by the listform. You can find the default rendering templates used by SharePoint in the 14 Hive or 15Hive in the controltemplate folder. It is the user control DefaultTemplates.ascx in which you’ll find the “Listform” template. Copy this one and add in your solution the mapped folder for controltemplates. In your user control add the copied template and rename the template id.

<sharepoint:renderingtemplate id="NewForm_Zaehlerstand" runat="server">
<span id='part1'><h3>Custom New Form</h3>
<sharepoint:informationbar ID="InformationBar1" runat="server"></sharepoint:informationbar>
<div id="listFormToolBarTop">
<wssuc:toolbar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator="&amp;amp;amp;#160;" runat="server">
<sharepoint:nextpagebutton ID="NextPageButton1" runat="server"></sharepoint:nextpagebutton>
<sharepoint:savebutton ID="SaveButton1" runat="server"></sharepoint:savebutton>
<sharepoint:gobackbutton ID="GoBackButton1" runat="server"></sharepoint:gobackbutton>

<sharepoint:formtoolbar ID="FormToolBar1" runat="server"></sharepoint:formtoolbar>
<sharepoint:itemvalidationfailedmessage ID="ItemValidationFailedMessage1" runat="server"></sharepoint:itemvalidationfailedmessage>
<table class="ms-formtable" style="margin-top: 8px;" border="0" cellpadding="0" cellspacing="0" width="100%">
<sharepoint:changecontenttype ID="ChangeContentType1" runat="server"></sharepoint:changecontenttype>
<sharepoint:folderformfields ID="FolderFormFields1" runat="server"></sharepoint:folderformfields>

<sharepoint:listfielditerator ID="ListFieldIterator1" runat="server"></sharepoint:listfielditerator>

<sharepoint:approvalstatus ID="ApprovalStatus1" runat="server"></sharepoint:approvalstatus>
<sharepoint:formcomponent ID="FormComponent1" TemplateName="AttachmentRows" runat="server"></sharepoint:formcomponent>
<table cellpadding="0" cellspacing="0" width="100%"><tr><td class="ms-formline"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr></table>
<table cellpadding="0" cellspacing="0" width="100%" style="padding-top: 7px"><tr><td width="100%">
<sharepoint:itemhiddenversion ID="ItemHiddenVersion1" runat="server"></sharepoint:itemhiddenversion>
<sharepoint:parentinformationfield ID="ParentInformationField1" runat="server"></sharepoint:parentinformationfield>
<sharepoint:initcontenttype ID="InitContentType1" runat="server"></sharepoint:initcontenttype>
<wssuc:toolbar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator="&amp;amp;amp;#160;" runat="server">
<sharepoint:createdmodifiedinfo ID="CreatedModifiedInfo1" runat="server"></sharepoint:createdmodifiedinfo>
<sharepoint:savebutton ID="SaveButton2" runat="server"></sharepoint:savebutton>
<sharepoint:gobackbutton ID="GoBackButton2" runat="server"></sharepoint:gobackbutton>

<sharepoint:attachmentupload ID="AttachmentUpload1" runat="server"></sharepoint:attachmentupload>

You also should add a register control of the “wssuc” which is needed:
<%@ Register TagPrefix="wssuc" TagName="ToolBar" src="~/_controltemplates/15/ToolBar.ascx" %>

Extended Properties in Word as Placeholder for custom values


How practically would it be if a word document could be filled with some values at predefined places (like placeholder)? In one of my customer projects the requirements were like this:

The customer manages his service agreements in a SharePoint list. Endusers type in the data in a normal SharePoint form. After that they can generate a word document with exactly these data. The data will be transformed into a word template for those agreements. So it is obvious that the data from the sharepoint listitem should be displayed and saved in the word template at certain predefined places. And this scenario can be true by using openxml.

Let me explain with an example more simple. We just insert values from c# into the word document. I’ll show you what you need to do in word and what you need to do in code. (more…)

MySQL Real Escape Function in C#

If you need a similiar function to the mysql_real_escape_string() from php, you might could use this one:

public static string MySQLEscape(string str)
return Regex.Replace(str, @"[\x00'""\b\n\r\t\cZ\\%_]",
delegate(Match match)
string v = match.Value;
switch (v)
case "\x00":            // ASCII NUL (0x00) character
return "\\0";
case "\b":              // BACKSPACE character
return "\\b";
case "\n":              // NEWLINE (linefeed) character
return "\\n";
case "\r":              // CARRIAGE RETURN character
return "\\r";
case "\t":              // TAB
return "\\t";
case "\u001A":          // Ctrl-Z
return "\\Z";
return "\\" + v;

Found it here. Here is another solution.

How can i sent mail with attachment from SharePoint?

Customer sometimes wanna send an email with their documents as attachment. If the documents are stored in a a SharePoint library, there is a small difference than if they are stored in a folder on the server. So this piece of code might help you to send the mail including the attached file from SharePoint library. The important step is to use the WebClient and Download Data method.

WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;

fileUrl = "your SharePoint File Path absolute";
byte[] data = webClient.DownloadData(fileUrl);
MemoryStream fs = new MemoryStream(data);

fileName = "Your filename.xx";

attachment = new Attachment(fs, fileName);

Hope it helps you.

How can i make attachments working in custom SharePoint listform?

If you use an application page as listform you’ll might notice that the button in the ribbon to attach files to the listitem does not work. It throws javascript errors like listform was customized… or something like id attachmentpart is missing or anything else. It might be that it displays nothing. So i thought – what a awesome story for 2014.

As i started my post serie about custom listforms i only thought about writing maybe 2 or 3 posts. This one will be my fifth post about custom listforms using visual studio and the application page way. Maybe i should make a post about the different ways to customize those forms.

But as short introduction let me give you the links to my previous posts:

Part 1: Setting up a solution with a list definition, a list instance and deploy it.

Part 2: Creating a custom list form and connect it with the list definition.

Part 3: Necessary settings for custom listform in Visual Studio

Part 4: Override Save Button in listform

Well, in order to make attachments working in your custom application page respective the listform page we’ll need to add certain parts with special id’s. Let’s see what parts we need.

Important notice: This has only relevance if you customizing also the fields. Means that you don’t use the webpartzone. To make it more clear: If you make the following part hidden or comment it out

<WebPartPages:WebPartZone runat=”server” FrameType=”None” ID=”Main” Title=”loc:Main” ><ZoneTemplate></ZoneTemplate> </WebPartPages:WebPartZone>

Then you have to manage that the attachments working. Let me show you how to do that.