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}

Donnerstag, 3. November 2011

What's New Webpart cross-site alternative

The SharePoint 2010 out of the box "What’s new" webpart is a nice feature unless the fact that it’s working only on the site level. So if you need something for a Site collection or cross site you need to look out for another solution. So here is my little workaround:
  1. Go to this website http://firearrowsoft.com/product_rss.php
  2. Install the free rss viewer Webpart on your sharepoint server.
  3. Activate RSS feed on your Sitecollection
  4. add the RSS WebPart to your sitecollection
  5. Configure the webpart by adding all important Libraries and Lists through there RSS-Feeds
and voilà!


Maybe it’s not the best solution but I think it’s a small, working and quick one

Mittwoch, 12. Oktober 2011

Lookup field is empty in datasheet view when created using object model

I created a lookup field in a List using the SharePoint object model. Everything worked fine except one strange behavior:
When I display the List in the Datasheet-View the Lookuplists were empty.
I found out that opening the field in the List-Settings and saving it again solve the problem.
So I compared the field object before and after saving it using the SharePoint Manager 2010.
The difference was the value of LookupField.
 
After adding this to my code:

spList.Fields[$fieldName].LookupField = "Title"

it worked fine.

Donnerstag, 29. September 2011

SharePoint 2010 customErrors mode

Are you getting crazy getting no error details even if you allready set customErrors to Off on the web.config?

Just read this Post, I think it may help you out:

SharePoint 2010 customErrors mode

The secret lays here:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\web.config

Montag, 19. September 2011

SharePoint Foundation Search not working

My SharePoint Foundation Search was not working. When I was searching for an Item in a list I was just getting this message "We did not find any results for host."

I started the SharePoint Foundation Search Service on the server.
I added the Managed Account.
And I started the SharePoint Foundation Search Refresh.

But I allways got the same message.

Then I looked  in the Event Viewer and I found following "SharePoint Foundation Search" Warning (Event ID: 14):

The start address sts4://win-u71mv1i4epi/contentdbid={521dee71-a532-4935-b906-5e9ed5f49d33} cannot be crawled.
Context: Application 'Search_index_file_on_the_search_server', Catalog 'Search'
Details:  The object was not found.   (0x80041201)

I found this Forum Entry: Search Problem with SharePoint Foundation on SBS 08
And there I found the source of my Problem: It was the alternate access mapping

here what I got in the central administration.

and here what I had in the IIS Manager

So I needed to add on binding and my problem was resolved. Here what it should look like:

Mittwoch, 7. September 2011

How to change the order in the SharePoint Foundation alternative SPSiteMapProvider

I was trying to improve the navigation for a customer without investing to much in money and time. I found a solution discribed in this blog:
Easy and Simple SharePoint 2010 Dropdown Navigation bar that works no scripts, no web.config, works on hosted providers

In summary: go to the masterpage and replace this part:
<SharePoint:AspMenu
      ID="TopNavigationMenuV4"
      Runat="server"
      EnableViewState="false"
      DataSourceID="topSiteMap"
      AccessKey="<%$Resources:wss,navigation_accesskey%>"
      UseSimpleRendering="true"
      UseSeparateCss="false"
      Orientation="Horizontal"
      StaticDisplayLevels="2"
      MaximumDynamicDisplayLevels="1"
      SkipLinkText=""
      CssClass="s4-tn"/>
    <SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource" Id="topNavigationDelegate">
        <Template_Controls>
            <asp:SiteMapDataSource
              ShowStartingNode="False"
              SiteMapProvider="SPNavigationProvider"
              id="topSiteMap"
              runat="server"
              StartingNodeUrl="sid:1002"/>
        </Template_Controls>
    </SharePoint:DelegateControl>

with this:
<SharePoint:AspMenu
 ID="SPSiteMapProvider"
 Runat="server"
 EnableViewState="false"
 DataSourceID="SiteMapDataSource1"
 AccessKey="<%$Resources:wss,navigation_accesskey%>"
 UseSimpleRendering="true"
 UseSeparateCss="false"
 Orientation="Horizontal"
 StaticDisplayLevels="2"
 MaximumDynamicDisplayLevels="3"
 SkipLinkText=""
 CssClass="s4-tn"/>
 <asp:SiteMapDataSource runat="server" ID="SiteMapDataSource1"/>
<SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource" Id="topNavigationDelegate">
 <Template_Controls>
 <asp:SiteMapDataSource
 ShowStartingNode="False"
 SiteMapProvider="SPSiteMapProvider"
 id="topSiteMap"
 runat="server"
 StartingNodeUrl="sid:1002"/>
 </Template_Controls>
</SharePoint:DelegateControl>
Unfortunately this Navigation provider sort the Items using ASCII order. He don't care about the order defined in the ToNavigationConfiguration interface.

So how can I sort the sites anyhow? Just add whitespaces to the title of the Web that you want to be on the first. Example:

Pages with the titles "First", "Second", "Third", "Fourth" and "Fifths" will be shown in this order:
"Fifths", "First", "Fourth", "Second", "Third"
To get them in the right order change the name to this:
"    First", "   Second", "  Third", " Fourth" and "Fifths"

And don't worry the whitespaces are trimmed in both the Navigation and the Page title.

It's not the most elegant way, but it works.

Enjoy and share