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

Visual Studio

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.


Did you know that you can remove buttons from SharePoint ribbon in code?

I am not sure if this behaviour is normal. I guess i have to investigate it another time. If you develop a listform using Visual Studio and based on an application page like i wrote in some other posts about it, you might notice, that in the edit and new form the save and cancel button does not work. They just do not firing the event.

The solution to this is really easy. In the schema file or by using code you change the listform url and also make the setting “UseLegacyForm” to true.

<form Type="NewForm" Url="NewForm.aspx" SetupPath="features\$SharePoint.Feature.DeploymentPath$\List1\NewSP.aspx" UseLegacyForm="TRUE" WebPartZoneID="Main"></form>

Doing so makes the save and cancel button working again, but now you have another stupid behaviour. Look at the screen: It is a generic / custom list as template, but it also shows buttons for edit series, claim release, distribution approval. What the hell is going on here?



What other user information can i get by user display name using c#?

Trying to get the e-mail adress of a user for whom you only have the display name like surname and name, you can get the user information by using the SPUtility function ResolvePrincipal. It’s quite easy but you have to ensure that

  • the user has access to the site in any kind
  • you check if “pInfo” in this example is not null before using it in code

Found it here.

SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, "John Smith", SPPrincipalType.User, SPPrincipalSource.All, web.AllUsers, false);

It’s worthy to check what other function SPUtility provides before writing your own methods.


What is a listfielditerator and how can i customize SharePoint listforms?

There are several ways to customize a listform in SharePoint. I wrote already about this topic previously a post serie (three parts). In this post i’d like to show you a different way to create custom list forms. This time i write about the so called listfielditerator and the rendering template.

What is a listfielditerator?

This part of SharePoint is responsible for the outcome of each field. With the field iterator it is possible to change some rendering behaviour as well as using the method isFieldExcluded. This method is used to exclude fields from the form. MSDN Article

What is a rendering template?

A rendering template defines the outcome of the whole aspx page for the listform. By the way, if i talk about listform i mean the display, edit and new form of a list or library. Back to the rendering template which defines the controls that have to be loaded and how they look alike. It also contains the listfielditerator.

Starting point:

First in the solution we need a mapped folder to the controltemplates folder in the 15 Hive of SharePoint. In this folder the ascx file is created to define the rendering template. We have a teamsite, in which are already some lists. We will just create an easy custom list with some columns and change its form.

After that we need a code file (.cs) in which create a class which inherits from Listfielditerator. Let’s see the details: