Archive

Posts Tagged ‘c#. .net’

SharePoint 2010: Configuring incoming emails on a Production Environment (non-Exchange Server)

September 17, 2012 1 comment

Recently I needed to setup incoming emails on a SharePoint 2010 site sadly, after researching this subject a lot, I was not able to find a blog that provided a complete answer of how to set this up on a Production Environment. There are some variations in the way this can be setup but the method I will be explaining in this blog involves a non-Exchange Server method.

Assumptions

I am assuming that:

  • You have setup an SMTP server on one of your SharePoint WFE’s (Web Front End) if you are not sure on how to do this then please check out this post.
  • You have a domain name registered and that you can modify the DNS records

The Solution

The solution involves:

  • Setting up the DNS Records
  • Configuring the SMTP Server
  • Configuring Central Administration
  • Configuring a SharePoint List

Setting up the DNS Records

In this example I am using a domain name: http://www.shareheaven.co.uk that is registered with GoDaddy.com.

Login to your domain name’s control panel and add a DNS A (Host) record in the following format:

Setting up a DNS A (Host) Record

Where the host (‘notify’ in this example) can be anything of your liking. The IP address should be the IP address of the server that hosts the SMTP Server.

Next we need to add a DNS MX Record in the following format:

Setting up the MX Record

Wait for the DNS changes to take effect, you can use this website to check if the changes have taken effect.

Configuring the SMTP Server

On the SMTP Server we will need to add an alias. Open IIS 6.0 Manager > Expand your SMTP Server in the list on the left hand side > Right click on ‘Domains’ > New > Domains.

SMTP Alias Setup

On this screen (screen shot above) select Alias and click Next and then fill it out as below (based on your domain name):

Creating an alias

Configuring Central Administration

Next we need to setup Central Administration to enable incoming emails in our SharePoint Farm, in Central Admin browse to System Settings > Configure incoming e-mail settings and set it up as below:

Central Administration Setup

Configuring a SharePoint List

Finally, we can now setup a SharePoint List to receive incoming emails. In this example I will be setting up a Document Library to receive incoming emails. Browse to Library Settings > Incoming e-mail settings of the SharePoint List you would like to setup to receive incoming emails. Fill out the form as below and click ‘OK’:

Configure the SharePoint List

Testing the Solution

To test the solution send an email to the email address we setup to receieve incoming emails which in our example was: test@shareheaven.co.uk (replace shareheaven with your domain name of course!):

Sending the email

Screen shot below shows the .eml file arriving in the drop folder of the SMTP Server:

Mail drop folder

A SharePoint Timer Job picks up this email, processes it and then adds an item in the Document Library:

Document Library Item Added

Archiving documents in SharePoint 2010 OTB

September 13, 2012 Leave a comment

There might be other ways of setting this up but I thought I should write about a way of achieving this by purely utilising SharePoint 2010 out-of-the-box features.

Scenario

You have a document library, we will call it Documents, that has a custom Content Type (we will call it ER Documents). Based on a certain criteria, lets say 6 months after a document has been approved, you would like to archive the document by moving it to another document library called ‘ER Compliance Archived Documents’.

Assumptions

You have two identical document libraries setup with the same content type enabled.

The Solution

The solution involves:

  • Enabling the Content Organizer Feature
  • Creating a Content Organizer Rule
  • Configuring a ‘send to’ connection
  • Configuring the Document Library

Enabling the Content Organizer Feature

On the root web of your Site Collection browse to Site Actions > Site Settings > Manage Site Features and activate the ‘Content Organizer’ Feature.

After this feature is activated you will notice two new options in Site Settings (highlighted in the screenshot below) and a new document library called ‘Drop Off Library’:

Content Organizor Options

Click on ‘Content Organizer Settings’ and make a note of the Web Service URL:

Web Service URL

Creating a Content Organizer Rule

Click on the ‘Content Organizer Rules’ link and add a new item to create a new rule and set it up as below (click on the image if it appears a bit blurred):

Content organizer rule

This rule basically specifies where and how to route the archived documents.

Configuring a ‘send to’ connection

Browse to Central Administration > General Application Settings > Configure send to connections. Select the correct Web Application and create a new send to connection by filling out the form as below pasting the Web Service URL you copied earlier:

Send To Connection

Click on ‘Add Connection’ and then ‘OK’.

Configuring the Document Library

Browse to the main ‘Documents’ document library > Library Settings > Information management policy settings > ER Documents (this is our content type) > Check ‘Enable Retention’ and fill the form out as below:

Enable Retention

Testing the solution

Add a document that matches the archiving criteria (i.e. Approval Date more than 6 months ago).

Go to Central Administration > Monitoring > Review job definitions > select your Web Application and manually run the ‘Information management policy’ timer job. This job process and marks the documents, that match the criteria we have setup, for transfer. After the job has completed manually run the ‘Expiration policy’ timer job this timer job does the actual transfer of the marked documents.

After this the relevant documents that match the Information management policy criteria will be moved to the archived library.

You can also test this manually by browsing to the ‘Documents’ document library> Accessing the context menu > Send to > ER Compliance Archive (screenshot below).

Manually archive document

The document should then appear in the archive document library. Please note that the manual send to method moves the document immediately but it is a useful way of testing whether you have configured the routing correctly.

SharePoint 2010: Basic List search / filter WebPart

April 19, 2012 61 comments

I have created a very simple SharePoint list search / filter WebPart which was inspired by the following blog post. This WebPart allows you to search records in a list where a selected field contains a specified text. It is useful in scenarios where you dont have SharePoint Search setup and just need a simple way of performing some search operations in a SharePoint List.

Adding the web part to SharePoint List View

Simply drop this web part on top of a page that contains a SharePoint View and it will allow you to apply a very simple search criteria.

The screenshot below shows the WebPart in action:

Main view of the List Search WebPart

The field name DropDownList allows you to select from the fields that are present in the view. Once you select the field and add the text to search by, the relevant results are displayed:

Screenshot showing search results

You can also specify multiple text values by seperating the text with a semi-colon (;):

Screenshot showing search results, multiple text criteria

In the above example the specified criteria will display all the records where the manufacturer’s name contains ‘Honda’ OR ‘Audi’. The screenshot below shows the pagination working as expected:

Screenshot showing search results, multiple text criteria

Adding the web part to a page with an XsltListViewWebPart

You can also add this web part to a page that contains an XsltListViewWebPart. The web part will automatically detect that it has been added to a page (rather than a List View) and display a message asking you to select an XsltListViewWebPart that you would like to apply the filters to:

The screen shot below shows how to select the XsltListViewWebPart:

You can download the solution by clicking on the link below:

SharePoint WSP Download link

You can view the codeplex project site by clicking on the link below:

Codeplex Project Site

Please note that this is setup as a Farm Solution and not a Sandboxed Solution therefore it will not work if you deploy it to the SharePoint Solutions Gallery, you need to deploy the SharePoint Solution via Central Administration, via stsadm commands or via PowerShell commands.

How to it works

On a page that contains a ListViewWebPart you can apply filter by adding a couple of query strings:

  • FilterName
  • FilterMultiValue

In our example, when a user types ‘honda’ and then clicks on the search button we simply append ‘?FilterName=LinkTitle&FilterMultiValue=*honda*;’ to the query string and redirect the user to that page. Please note that ‘LinkTitle’ is the internal name of the ‘Manufacturer’ field.

The * in the *honda* is used to do a wildcard search (contains). If you would like to search for multiple texts you can seperate them by a semi-colon for example ‘FilterMultiValue=*honda*;*audi*;’ will search for records where the ‘Manufacturer’ name either contains ‘honda’ or ‘audi’. If you would like to search for an exact match rather than apply a contains filter then simply remove the *’s from the filter value text.

Although, this WebPart does not allow you to filter / search by more than one field this is very much possible. To apply filters on additional fields you simply need to append ‘FilterField1=Model&FilterValue1=Accord’ to the URL. You can apply further filters by incrementing the number i.e. FilterField2, FilterField3 …. and so on. I am not sure if there is a limit on this.

Please note that I haven’t found a way to get the wildcard search to work with this (multiple filters) approach.

Building the WebPart

In your Visual Studio solution (assuming you have created a Blank SharePoint Project) add a ‘Visual WebPart’. A Visual WebPart loads a UserControl that contains most of the code. Below is the code of the .ascx file:

<%@ 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="ListSearchUserControl.ascx.cs"
    Inherits="Exaction.ListSearch.WebParts.ListSearch.ListSearchUserControl" %>
<script type="text/javascript" src="/_layouts/Exaction.ListSearch.Javascripts/jquery.min.js"></script>
<table>
    <tr>
        <td>
            <strong>Search Criteria:</strong>
        </td>
        <td>
            <asp:TextBox ID="TbSearchText" runat="server" Width="300px"></asp:TextBox>
        </td>
        <td>
            &nbsp;
        </td>
        <td>
            <strong>Field name:</strong>
        </td>
        <td>
            <asp:DropDownList ID="DdlListFields" runat="server">
            </asp:DropDownList>
        </td>
        <td>
            &nbsp;
        </td>
        <td>
            <div align="right">
                <asp:Button ID="BtnSearch" runat="server" OnClick="BtnSearch_Click" Text="Search" />
                <asp:Button ID="BtnClearFilter" runat="server" Visible="false" OnClick="BtnClearFilter_Click"
                    Text="Clear Criteria" />
            </div>
        </td>
    </tr>
</table>
<script type="text/javascript">
    $(document).ready(function () {
        var base_RefreshPageTo = RefreshPageTo;
        RefreshPageTo = function (event, url) {

            var filterName = getQuerystring('FilterName');
            var filterValue = getQuerystring('FilterMultiValue');
            var newUrl = url + '&FilterName=' + filterName + '&FilterMultiValue=' + filterValue;
            if (filterName != '' && filterValue != '') {
                base_RefreshPageTo(event, newUrl);
            }
            else {
                base_RefreshPageTo(event, url);
            }
            return;
        }
    });
    function getQuerystring(key, default_) {
        if (default_ == null) default_ = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return default_;
        else
            return qs[1];
    }
</script>

The code above is pretty self-explanatory but very briefly it contains the UI elements (TextBox, Labels, DropDownList and Buttons) and some jQuery. The jQuery code overrides the ‘RefreshPageTo’ SharePoint javascript function. This is basically to get our filtering to work with pagination. If you have a SharePoint List View that is displaying paginated date to you then clicking on the next or previous page calls the ‘RefreshPageTo’ JavaScript function. The problem is that when this function is called it clears the querystrings we use to filter the data. To ensure that the filtering is maintained we override this function, modify the URL ensuring the filtering querystrings are present and then pass it in as the second parameter to the function.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using System.Collections.Generic;
using Exaction.ListSearch.UI.Entities;
using System.Text;

namespace Exaction.ListSearch.WebParts.ListSearch
{

    /// <summary>
    /// User control that deals with the registration process
    /// </summary>
    public partial class ListSearchUserControl : UserControl
    {

        /// <summary>
        /// Gets the share point list field items.
        /// </summary>
        /// <param name="filterCriteria">The filter criteria.</param>
        private List<OptionEntity> GetSharePointListFieldItems()
        {
            List<OptionEntity> fieldItems = new List<OptionEntity>();
            fieldItems = new List<OptionEntity>();
            OptionEntity item;
            SPField field;
            StringCollection viewFieldCollection = SPContext.Current.ViewContext.View.ViewFields.ToStringCollection();
            foreach (string viewField in viewFieldCollection)
            {
                field = SPContext.Current.List.Fields.GetFieldByInternalName(viewField);
                item = new OptionEntity();
                item.Id = field.InternalName;
                item.Title = field.Title;
                fieldItems.Add(item);
            }
            return fieldItems;
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            List<OptionEntity> items = GetSharePointListFieldItems();
            DdlListFields.DataSource = items;
            DdlListFields.DataTextField = "Title";
            DdlListFields.DataValueField = "Id";
            DdlListFields.DataBind();
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void  OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                if (Request.QueryString["FilterName"] != null)
                {
                    DdlListFields.SelectedValue = Request.QueryString["FilterName"].ToString();
                }

                if (Request.QueryString["FilterMultiValue"] != null)
                {
                    TbSearchText.Text = Request.QueryString["FilterMultiValue"].ToString().Replace("*", "");
                    BtnClearFilter.Visible = true;
                }
            }
        }
        /// <summary>
        /// Handles the Click event of the BtnSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            string redirectUrlFormat = "{0}?FilterName={1}&FilterMultiValue={2}";
            string[] selectionCollection = TbSearchText.Text.ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sbValues = new StringBuilder();
            foreach (string selection in selectionCollection)
            {
                sbValues.Append("*" + selection.Trim() + "*;");
            }

            string urlToRedirectTo = string.Format(redirectUrlFormat, Request.Url.GetLeftPart(UriPartial.Path), DdlListFields.SelectedValue, sbValues.ToString());
            Response.Redirect(urlToRedirectTo);
        }
        /// <summary>
        /// Handles the Click event of the BtnClearFilter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void BtnClearFilter_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
        }
    }
}

The code behind above initialises the controls and handles the Search and Clear Search Criteria Button click events.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exaction.ListSearch.UI.Entities
{
    public class OptionEntity
    {
        #region "Fields"
        public string Id { get; set; }
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }
        #endregion

        #region "Constructor"
        public OptionEntity()
        {
        }
        #endregion
    }
}

We set a collection of the OptionEntity items as the DataSource of the Field name DropDownList.

That is basically it. In this simple manner you have a WebPart that you can drop on top of any List View and apply some basic free text Filterting.

Known issues

There are two minor known issues which I haven’t found a solution for yet:

  • Adding the WebPart on top of the page of a List View takes the focus away from the ListViewWebPart which in turn hides the ribbon. Once you click on the ListViewWebPart and focus on it then the ribbon becomes visible.
  • This WebPart does not work properly with Views that use groupings that are collapsed by default, it works if the groupings are expanded by default
  • As pointed out by Goran (see comments) it might not work with External SharePoint Lists

I hope you find this WebPart useful. Please post your comments and feedback and it would be helpful if you can rate this post.

ASP.NET LinkedListBoxes UserControl

January 5, 2012 Leave a comment

I needed a simple Linked ListBox control for a project but couldnt find one that was available for free so I ended up developing one myself.

I have created a project for it on codeplex and published it. Please feel free to use/modify the control as you require. If you have any suggestions or feedback then please feel free to leave a comment.

The control uses two ListBoxes with two buttons (add, remove) in between that allows you to add and remove items by using some Javascript. The screen shot below shows the control in action:

LinkedListBox UserControl

You can download the complete source code from the link below:

LinkedListBox UserControl sourcecode.

You can also download a sample Visual Studio 2010 project that shows the control in action by going to the project home page and clicking on the Download button:

Project homepage

How to change the Author of a Document that is stored in a Document Library?

April 20, 2009 7 comments

Apparently there is’nt a straight forward way to achieve this. Below is how you can do it though:

SPList list = web.Lists["myList"];

list.Fields["Author"].ReadOnlyField = false;

SPListItem item = list.Items.Add();

item["Author"] = “value”;

item.SystemUpdate(false);

list.Fields["Author"].ReadOnlyField = true;

WebPartPages: Programmatically adding a new web part page

April 5, 2009 2 comments

How do you add a WebPartPage or a BasicPage programmatically?

WebPartPages are stored in document libraries. It is simply a matter of adding the page to the document library you want. However, if you need to add a WebPartPage programmatically the same way SharePoint allows you to do via the UI then you can use the following method:


private void AddWebPartPage(string fileTitle, SPWeb web, SPList list, int webPartPageTemplate, string pageType, string folder)
{
    string postInformation =
    "<?xml version="1.0" encoding="UTF-8"?>" +
    "<Method>" +
    "<SetList Scope="Request">" + list.ID + "</SetList>" +
    "<SetVar Name="ID">New</SetVar>" +
    "<SetVar Name="Cmd">NewWebPage</SetVar>" +
    "<SetVar Name="Type">" + pageType + "</SetVar>" +
    "<SetVar Name="WebPartPageTemplate">" + webPartPageTemplate + "</SetVar>" +
    "<SetVar Name="Title">" + fileTitle + "</SetVar>" +
    "<SetVar Name="Overwrite">true</SetVar>" +
    "</Method>";
    string processBatch = web.ProcessBatchData(postInformation);
    if (processBatch.Equals("<Result ID="" Code="0">rn</Result>n"))
    {
        SPFile file = web.GetFile(list.RootFolder.Url + "/" + fileTitle + ".aspx");
        //if page was in subfolder then move it there
        if (!String.IsNullOrEmpty(folder))
        {
            file.MoveTo(fileTitle,
true);
        }
    }
}

Below are the WebPartPage templates you can specify:

  1. Full Page, Vertical
  2. Header, Footer, 3 Columns
  3. Header, Left Column, Body
  4. Header, Right Column, Body
  5. Header, Footer, 2 Columns, 4 Rows
  6. Header, Footer, 4 Columns, Top Row
  7. Left Column, Header, Footer, Top Row, 3 Columns
  8. Right Column, Header, Footer, Top Row, 3 Columns

Type could have the following possible values:

  1. WebPartPage
  2. BasicPage

For further details click here.

ListViewWebPart: Programmatically setting the ToolbarType property

April 5, 2009 7 comments

While working on a migration project we came across a requirement whereby ListViewWebPart’s had to be added to a page programmatically. The ListViewWebPart class has a property called toolbartype. Setting the toolbartype property sounds simple enough. I mean how difficult can it be to set a property?

The problem is that it is a read only property. We thought maybe we can set it via reflection. After searching a bit on google we found a couple of suggestions on how it could be done (most of them were solutions on how to set it to none). One method seemed to have solved our problem until we installed the latest infrastructure updates. Then it completely stopped working. However luckily for us Tony Stegeman and Brian Farhill came up with a solution.

Below you can see the complete solution that worked for us:


private static void SetToolbarType(SPView spView, string toolBarType)
{
    spView.GetType().InvokeMember(
"EnsureFullBlownXmlDocument",
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
    null, spView, null, System.Globalization.CultureInfo.CurrentCulture);
    PropertyInfo nodeProp = spView.GetType().GetProperty("Node",
    BindingFlags.NonPublic | BindingFlags.Instance);
    XmlNode node = nodeProp.GetValue(spView, null) as XmlNode;
    XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
    if (toolbarNode != null)
    {
        toolbarNode.Attributes[
"Type"].Value = toolBarType;
        // If the toolbartype is Freeform (i.e. Summary Toolbar) then we need to manually 
        // add some CAML to get it to work.
        if (String.Compare(toolBarType, "Freeform", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
        {
            string newItemString = "";
            XmlAttribute positionNode = toolbarNode.OwnerDocument.CreateAttribute("Position");
            positionNode.Value =
"After";
            toolbarNode.Attributes.Append(positionNode);
            switch (spView.ParentList.BaseTemplate)
            {
                case SPListTemplateType.Announcements:
                    newItemString =
"announcement";
                    break;
                case SPListTemplateType.Events:
                    newItemString =
"event";
                    break;
                case SPListTemplateType.Tasks:
                    newItemString =
"task";
                    break;
                case SPListTemplateType.DiscussionBoard:
                    newItemString =
"discussion";
                    break;
                case SPListTemplateType.Links:
                    newItemString =
"link";
                    break;
                case SPListTemplateType.GenericList:
                    newItemString =
"item";
                    break;
                case SPListTemplateType.DocumentLibrary:
                    newItemString =
"document";
                    break;
                default:
                    newItemString =
"item";
                    break;
            }
            if (spView.ParentList.BaseType == SPBaseType.DocumentLibrary)
            {
                newItemString =
"document";
            }
            // Add the CAML
            toolbarNode.InnerXml = @"<IfHasRights><RightsChoices><RightsGroup PermAddListItems=""required"" /></RightsChoices><Then><HTML><![CDATA[ <table width=100% cellpadding=0 cellspacing=0 border=0 > <tr> <td colspan=""2"" class=""ms-partline""><IMG src=""/_layouts/images/blank.gif"" width=1 height=1 alt=""""></td> </tr> <tr> <td class=""ms-addnew"" style=""padding-bottom: 3px""> <img src=""/_layouts/images/rect.gif"" alt="""">&nbsp;<a class=""ms-addnew"" ID=""idAddNewItem"" href=""]]></HTML><URL Cmd=""New"" /><HTML><![CDATA["" ONCLICK=""javascript:NewItem(']]></HTML><URL Cmd=""New"" /><HTML><![CDATA[', true);javascript:return false;"" target=""_self"">]]></HTML><HTML>Add new " + newItemString + @"</HTML><HTML><![CDATA[</a> </td> </tr> <tr><td><IMG src=""/_layouts/images/blank.gif"" width=1 height=5 alt=""""></td></tr> </table>]]></HTML></Then></IfHasRights>";
        }
        spView.Update();
}

Programmatically getting the SPField object you just added

April 3, 2009 3 comments

When you add an SPField to an SPList programmatically using the SharePoint Object Model you can get the SPField object representing the newly added field via SPList.Fields[string displayName]. But this assumes that no other field with the same display name as the newly added SPField exists in the SPList.

The work around is listed below:

 

Guid fieldGuid = Guid.NewGuid();
string newFieldCAML =
 "<Field Type="Text" DisplayName="MyNewField" ID="+fieldGuid+"/>";
splist.Fields.AddFieldAsXml(newFieldCAML, false, SPAddFieldOptions.AddFieldInternalNameHint);
SPField newField = spList.Fields[fieldGuid];

You can use the AddFieldAsXml to add the new SPField. It takes a CAML string as a parameter in which you can specify your own Guid. Later you can use this Guid to retrieve the SPField you just added.

Meeting Workspace: How to programatically add tabs

April 3, 2009 1 comment

When you add a page via the UI it adds it as a WebPartPage to a hidden list called “Workspace Pages”. It also does something internally to create the Tab because simply progammatically adding the WebPartPage to the hidden list doesnt seem to work.

After spending a lot of time investigating this issue I finally found a solution. The solution is listed below:
 

string newPage = string.Empty;
Type cmType = typeof(SPMeeting);
SPMeeting mtg = (SPMeeting)
Activator.CreateInstance(cmType,
BindingFlags.NonPublic | BindingFlags.Instance,
null, null, System.Globalization.CultureInfo.CurrentCulture,
null);
Type t = mtg.GetType();
t.InvokeMember(
"m_Web", BindingFlags.NonPublic
|
BindingFlags.Instance | BindingFlags.SetField,
null, mtg, new Object[] { web });
mtg.AddPage(
"JunkYard", 1, out newPage);

The SPMeeting class has a method “AddPage”. This class has no public constructors therefore you cannot instantiate it the normal way. Once you instantiate it you need to set, via reflection, m_Web which is a private member and holds the SPWeb object. The final step is to call the AddPage method passing it the name of the page you want to add, an instanceid (not sure what this is), and an out string parameter.

SharePoint issues and work arounds!

April 3, 2009 1 comment

Cannot set Internal Name of an SPField:

The InternalName property is read only. According to the documentation the StaticName property of an SPField should:

“Get or set the internal name of the field. “

It doesnt! We had a requirement where we needed to set the InternalName of an SPField. We tried everything! various methods, reflection you name it but to no avail!

Trouble with getting SPField object after programmatically adding it:

There are a few ways you can add an SPField to a List.:

  1. SPField.Add() has three overloads
  2. SPField.CreateNewField()
  3. AddFieldAsXml() has 2 overloads

Using SPField.Add you can add an SPField by providing the DisplayName, Type and a bool specifying whether the field is required or optional. But how do you get a reference to the SPField you just added when you use this method?

You could use:

  1. SPField[int index] but you dont know the index
  2. SPField[Guid id] but you dont know the Guid
  3. SPField[string DisplayName] you do know the DisplayName but what if there is another field with the same DisplayName since there is no restriction on having more than 1 fields with the same DisplayName?
  4. SPField.GetField(string displayName) same problem as above
  5. SPField.GetFieldByInternalName(string internalName) but we dont know the internal name (linked with the issue mentioned above. This is why we needed to be able to set the InternalName).

If there was a GetFieldByStaticName it could have solved our problem. One of the overloads of the SPField.Add() method takes an SPField. We thought we would instantiate and setup our SPField object, pass it to that method to add to the List and we will then have a proper reference of our newly added SPField object right? No wrong! The SPField object is just a means to send in the data required to set up the new SPField.

It would be useful to have a method that will add an SPField and return an SPField object representing the SPField you just added. However in the absence of this there is a way around this whole issue. The way around it is to use the AddFieldAsXml() method. It takes a string argument representing the CAML for the SPField you are trying to add. In that you can specify the Guid you want for your new field. Later you can retrieve your field by using SPField[Guid id].

You can find the solution here.

Basic issue all over the place in the SharePoint Object Model:

You are adding an SPList programmatically. You want to find out whether an SPList with that name already exists. What do you do? You could try:

  1. SPList[string listName]
  2. SPWeb.GetList(string listName)
  3. SPWeb.GetListFromUrl(string pageUrl)

If the list exists it will get you the SPList object representing that list. If it doesnt it throws an exception! So if you dont have a try catch block when checking if a List with a particular name already exists your application will crash!

I think there is a need for a method which will return a particular value based on whether an SPList with that name exists (could return a bool, or the SPList object if it exists or null if it doesnt).

When adding an SPList you need to check whether an SPList with the same name exists or not because if it does then the SPList.Add() method will throw an exception. In other words it all works perfectly if you add a List and by chance the name is unique. If it is not unique then an error will be thrown and must be caught to prevent your application from crashing.  Surely, you should be able to check whether a list exists without having to catch an error!

This issue manifests itself in a lot of places in the object model. You have the same problem when trying to add an SPField, SPView e.t.c.

While programmatically adding a ListViewWebPart you cannot set the ToolbarType property:

There is a property called ToolbarType on a ListViewWebPart which is read only. There is a way around this issue which involves using reflection.

The work around for this is discussed here.

No clear way of adding Tabs to a Meeting Workspace Site:

When you create a site from the OTB Meeting workspace site definitions you are able to add pages to the site. The pages are stored in a hidden list called Workspace pages. When you add a page it adds a tab for that page at the top. However adding a WebPart page to this list programmatically doesnt solve the problem as it will not add the tab. There is a very painful way around this and it involves Reflection!

The solution is here.

Adding a WebPartPage to an SPList sub folder:

There is a way of adding it to the root folder but there doesnt seem to be a direct way of adding it to a sub folder. Again there is a way around this. You add the WebPartPage to the root folder of the SPList and then move it to the sub folder you actually wanted to add it to.

You can find a solution for this here.

Cannot get WebParts from a MySite (SPS 2003):

Not sure if its the same on MOSS 2007 but when we tried to get WebParts from the default page on a Personal Site (unless its your Personal Site) you wont be able to get an instance of the WebParts that exist on the page. Raised this issue with Microsoft who said this was for security reasons. However no such restrictions when trying to get data from the lists on the Personal Site :) Run with elevated priviledges makes no difference either. The only way around this is to impersonate the user whose Personal Site it is or just give up!

Follow

Get every new post delivered to your Inbox.