Donnerstag, 29. Dezember 2011

How to get Name and other Information of the provider in the consumer WebPart

I needed to display the name of the provider webpart in a consumer WebPart when working with WebPartConnections. 

Here the code to access provider Information from the consumer WebPart:

SPWeb myWeb = SPControl.GetContextWeb(Context);
SPLimitedWebPartManager webPartManager = myWeb.GetLimitedWebPartManager(this.Page.Request.FilePath, PersonalizationScope.Shared);


foreach (SPWebPartConnection connection in webPartManager.SPWebPartConnections)
{
    if(writer != null)
        writer.Write("Provider: " + connection.Provider.Title
+ " - Consumer: " + connection.Consumer.Title + "<br />");
}


Mittwoch, 21. Dezember 2011

Error when placing a TextBox inside a LayoutsPageBase

I was developing an application page that inherits from the LayoutsPageBase. 
In this page I added a user Control in which I added A TextField and a Button. Here is the code for that part:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ExportApplicationSettingsUserControl.ascx.cs" Inherits="ListItemExport.ControlTemplates.ListItemExport.ExportApplicationSettingsUserControl" %>
<asp:TextBox ID="exportTemplate" runat="server"></asp:TextBox>
<br />
<asp:Button ID="saveButton" runat="server" onclick="saveButton_Click" Text="Button" />

When Executing this code I got following error message:

Control 'ctl01_exportTemplate' of type 'TextBox' must be placed inside a form tag with runat=server.

So I added the form tag around my form elements like this:

<form id="myForm" runat=server></form>
<asp:TextBox ID="exportTemplate" runat="server"></asp:TextBox>
<br />
<asp:Button ID="saveButton" runat="server" onclick="saveButton_Click" Text="Button" />
</form>

This time a got following error message:

A page can have only one server-side Form tag.

It came out that the masterpage already had a form tag. I tried to delete the form tag from the masterpage or to include other masterpage, but nothing helped. There were always new errors appearing.
Finally I found the solution for my problem. It appeared that this was simply a bug. The solution was to add following empty method to the application page code behind:


public override void VerifyRenderingInServerForm(Control control){}

Don’t forget to delete the added form tag in the application page. And don’t make any changes on the masterpage.
This solved the problem. Thanks to this Post 

Create xml file from string and downloading it with save dialog

I needed to create an XML programmatically and then to send it to the user to download. For this purpose I created a Page that is called from a customAction. Here is the code for the xml downloading part.
Aspx:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportApplication.aspx.cs" Inherits="ListItemExport.Layouts.ListItemExport.ExportApplication" Debug="true" %>

Code Behind:
namespace ListItemExport.Layouts.ListItemExport
{
    public partial class ExportApplication : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e){
            base.OnLoad(e);
            String xmlString = "<xml><test>testvalue</test></xml>";

            String listId = Request.Params["listId"];
            String itemId = Request.Params["itemId"];

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xmlString);

            MemoryStream xmlStream = new MemoryStream();
            xDoc.Save(xmlStream);
            Response.Clear();
            Response.AppendHeader("Content-Disposition","attachment; filename=test.xml");
            Response.ContentType = "text/XML";
            Response.BinaryWrite(xmlStream.ToArray());

        }
    }
}

Freitag, 16. Dezember 2011

Sharepoint WebPart Lifecycle Events

When developing SharePoint WebPart you absolutely need to know the WebPart Lifecycle Events.

I found this great description on Phil Harding site. Here you can find the original Post: Sharepoint WebPart Lifecycle Events

Dienstag, 13. Dezember 2011

How to get SharePoint List ID not HTMLEncoded

When you try to get a List ID in SharePoint you will always go to Library Settings and get it from the URL. There will be something like this:

listedit.aspx?List=%7B618C96DB%2DE1C5%2D4C7D%2D9B69%2D2AEB4702008D%7D

Problem here is, that you need to replace different Strings (%7B, %7D, %2D) this is not really a problem but it is a waste of time.

In the List Settings (or library Setting) just click on "Information management policy settings". Here you can get your clean decoded List ID from the URL

Example:

List={618c96db-e1c5-4c7d-9b69-2aeb4702008d}