Sharepoint List item attachments validating in #spforms

Working with list item attachments is not the best option, but it makes sense at certain points. For example it is a good place to store a pdf-version of your list item or other additional information to the listitem.

If you add an attachment to the list, SharePoint stores the listitem and then tries to upload your attachment. If the attached file has an incorrect filename or characters which are not allowed, the file will not be attached, but the list item will be saved. That’s not so cool, if you’re running workflows on your list.

One possibility is to validate the file name before the save event occurs. Continue reading

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" %>
Continue reading

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. Continue reading

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.