SharePoint Dragons

Nikander & Margriet on SharePoint

Tag Archives: sharepoint2010

Accessing a WCF service that is SharePoint context aware using the WCF channel factory

As discussed in forum thread http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/42f01511-3083-4d19-9058-00cf56af0fb7 , this is in fact an elegant way to reference a WCF service in the context of a given SharePoint web site without needing to change the web.config file of the SharePoint web application (this minimizing the overhead of managing custom software):

//Access WCF service Centre Detail Endpoint

WSHttpBinding binding = new WSHttpBinding();

EndpointAddress address = new EndpointAddress(SPContext.Current.Web.Url + “/_vti_bin/MyService/MyService.svc”);

ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address); IGetCentreNames client = factory.CreateChannel();

Keep relational data in a database or migrate it into a custom list

It’s an interesting question: should you keep relational data in a database or migrate it into a custom list? We’d love our readers to contribute to the discussion at the TechNet Wiki page at http://social.technet.microsoft.com/wiki/contents/articles/9638.sharepoint-2010-best-practice-keep-relational-data-in-a-database-or-migrate-it-into-a-custom-list.aspx

The discussion as it stands so far:

If you need relational database capabilities (transactionality, triggers, real constraints (not the approximation of that in SharePoint)): use the existing database (and create a BCS external list to access the data).

– Are there existing apps using the current relational database? If yes, create a BCS external list to access the data.

– If no reasons prevent it, migrate the data into a custom list as this makes the solution less complex.

You need to make sure you play nicely and keep the data within the limits of capacity planning (https://sharepointdragons.com/2011/12/05/sharepoint-capacity-planning/ ). If there is absolutely no way that you can’t, keep the data where it is.

Best practices regarding row limits in external lists

When working with external lists, a functionality of Business Connectivity Services (BCS), you’ve got to keep an eye on row limits.

Keep in mind two important limits:

  1. The default Max is 2,000 items
  2. The absolute Max is 25,000 items

Please see this link for more information: http://msdn.microsoft.com/en-us/library/hh144965.aspx

You should always define filters (it can be limit/comparison/wildcard filter) so that you can narrow your search and overcome others restrictions regarding a count of rows in the database. Especially when working with high volume of items, there might be cases in which you may have millions of items to pick, the bcs picker will show up to 200 items (this is a SharePoint restriction).

Please take a look at these articles about filter creation:

Also you may implement paging. Please take a look at these articles:

This info was taken from forum discussion: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010general/thread/9dba356c-65f3-49be-a452-6934baee7216

For ongoing work on this topic, check out the latest version of Wiki page http://social.technet.microsoft.com/wiki/contents/articles/9628.sharepoint-2010-best-practice-row-limits-in-external-lists.aspx

An editor part for a visual web part

This is a follow up to blog post https://sharepointdragons.com/2012/04/06/a-web-part-editor-part-containing-a-drop-down-list/ . Here, we’ll create the same thing, but this time for a visual web part. The differences are minor, but still…

First, we’ve created a user control containing a simple label, just so we’ll be able to show something:

<%@ 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=”VisualWebPart1UserControl.ascx.cs” Inherits=”VisualWebPartProject1.VisualWebPart1.VisualWebPart1UserControl” %>
<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>

The code behind for this user control contains a public string property. All the control does is display the value of this property in the label:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VisualWebPartProject1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        public string MyLabelText { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = MyLabelText;
        }
    }
}

The editor part remains exactly as it was:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

namespace VisualWebPartProject1.VisualWebPart1
{
    public class WebPart1EditorPart : EditorPart
    {
        // Reference to the web part that uses this editor part, the parent web part needs to implement the IWebEditable interface to support custom editing controls
        protected VisualWebPart1 ParentWebPart { get; set; }
        protected DropDownList EditorChoices { get; set; }

        /// <summary>
        /// Does editor part init settings
        /// </summary>
        public WebPart1EditorPart()
        {
            Title = “Make a choice – good name here”;
        }

        /// <summary>
        /// Creates the dll
        /// </summary>
        protected override void CreateChildControls()
        {
            EditorChoices = new DropDownList();
            EditorChoices.Items.Add(new ListItem(“AAA”, “1”));
            EditorChoices.Items.Add(new ListItem(“BBB”, “2”));
            EditorChoices.Items.Add(new ListItem(“CCC”, “3”));
            Controls.Add(EditorChoices);

            base.CreateChildControls();
            ChildControlsCreated = true;
        }

        /// <summary>
        /// Reads current value from parent web part and show that in the ddl
        /// </summary>
        public override void SyncChanges()
        {
            EnsureChildControls();
            ParentWebPart = WebPartToEdit as VisualWebPart1;

            if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
            {
                ListItem item = EditorChoices.Items.FindByValue(ParentWebPart.MyEditorPartChoice);
                if (item != null) item.Selected = true;
            }
        }

        /// <summary>
        /// Applies change in editor part ddl to the parent web part
        /// </summary>
        /// <returns></returns>
        public override bool ApplyChanges()
        {
            try
            {
                EnsureChildControls();
                ParentWebPart = WebPartToEdit as VisualWebPart1;

                if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
                {
                    ParentWebPart.MyEditorPartChoice = EditorChoices.SelectedValue;
                }

                // The operation was succesful
                return true;
            }
            catch
            {
                // Because an error has occurred, the SyncChanges() method won’t be invoked.
                return false;
            }
        }
    }
}

The web part (ours is called VisualWebPart1.cs) that is responsible for loading the user control is a little different from what it was. Now, it has to cast the user control to our specific type and set our custom string property to the value that was retrieved from our editor part:

Control control = Page.LoadControl(_ascxPath);

var myVisualControl = control as VisualWebPart1UserControl;
if (MyEditorPartChoice == null)
myVisualControl.MyLabelText = “Make a choice first”;
else
myVisualControl.MyLabelText = “The value from the editor part choice: ” + MyEditorPartChoice;

Controls.Add(control);

The complete code for VisualWebPart1.cs looks like this:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace VisualWebPartProject1.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart, IWebEditable
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @”~/_CONTROLTEMPLATES/VisualWebPartProject1/VisualWebPart1/VisualWebPart1UserControl.ascx”;

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);

            var myVisualControl = control as VisualWebPart1UserControl;
            if (MyEditorPartChoice == null)
                myVisualControl.MyLabelText = “Make a choice first”;
            else
                myVisualControl.MyLabelText = “The value from the editor part choice: ” + MyEditorPartChoice;

            Controls.Add(control);
        }

        /// <summary>
        /// Contains the value of the choice in the editor part drop down list
        ///
        /// Set the Personalizable attribute to true,
        /// to allow for personalization of tabs by users.
        /// This causes  this property to be shown in the web part tool pane and lets SharePoint take care of the storage/retrieval of this property in the SharePoint content database.
        /// </summary>
        [Personalizable(true)]
        public string MyEditorPartChoice { get; set; }

        /// <summary>
        /// Creates custom editor parts here and assigns a unique id to each part
        /// </summary>
        /// <returns>All custom editor parts used by this web part</returns>
        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            var editors = new List<EditorPart>();
            var editorPart = new WebPart1EditorPart();
            editorPart.ID = ID + “_editorPart”;
            editors.Add(editorPart);

            return new EditorPartCollection(editors);
        }

        /// <summary>
        /// Returns parent web part to editor part
        /// </summary>
        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }

    }
}

SharePoint Bloggers to follow

Interesting post containing a list of SharePoint Bloggers Mark Miller thinks you should follow because they frequently provide content of high value: https://www.nothingbutsharepoint.com/sites/eusp/Pages/20-SharePoint-Blogs-to-Follow.aspx Of course, we whole heartedly agree being listed here Smile, although to avoid people getting the wrong idea (especially people considering to hire us! please note the shameless plug for our consulting business) we should add that we really are developers/architects, not IT Pros.

Moving your taxonomy to a production environment

If you’re planning to move your taxonomy to a production environment in SPS 2010, consider using the Bulk TermSet Importer-Exporter: http://termsetimporter.codeplex.com/releases/view/48669#ReviewsAnchor

WinForm SharePoint list item editing tool

A free tool for doing mass upload and bulk edit in SharePoint: http://splistitemeditor.codeplex.com/releases/view/35050 We’ll have to give it a test spin, but it might be interesting to consider if you need this kind of functionality.

Gotcha for validating the textfield of the SharePoint DateTime control

If you’re planning to use the SharePoint date time control. a good place to start is the followiing: http://karinebosch.wordpress.com/sharepoint-controls/datetimecontrol-control/ It won’t tell you how to use validation controls in combination with it, and there’s an important gotcha involved. If you want to access the textfield rendered by the SharePoint date time control, you need to set the ControlToValidate property to the name of the control in the following format: [control id]$[control id]Date, for example: MyControl$MyControlDate, like so:

<SharePoint:DateTimeControl runat=”server” ID=”MyControl” DateOnly=”true” ShowWeekNumber=”true” LocaleId=”1043″ IsRequiredField=”true” />
<SharePoint:InputFormCustomValidator runat=”server” ID=”MyCustomValidator” ControlToValidate=”MyControl$MyControlDate”
Display=”Dynamic” SetFocusOnError=”true” OnServerValidate=”ValidateFromDateControl” />

Also see: http://www.greggalipeau.com/2008/06/27/sharepoint-datetimecontrol-validation/

Solving problems with ULS log file generation

If the ULS log files remain empty (0 KB) take the following steps to resolve:

  • Make sure you have enough space in your hardware driver.
  • Check timer and tracing service accounts information, the tracing service must use the account as Local Service.
  • Reset the timer service account password. Restarted the tracing service.
  • Check Diagnostic logging under Central Administration site, you can refer to:http://technet.microsoft.com/en-us/library/ee748656.aspx
  • Check the permissions on /12/logs folder

And these blogs may help you:

http://sharepointlearningcurve.blogspot.com/2010/04/sharepoint-2010-uls-problems-logs-are.html

http://www.eiben.weite-welt.com/2011/08/no-uls-logging-in-sharepoint-2010/

Taken from forum thread: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010general/thread/e327f833-5e43-42d1-a330-f2d16c41106a

Check the following Wiki page for updates: http://social.technet.microsoft.com/wiki/contents/articles/9340.sharepoint-2010-solving-problems-with-uls-log-file-generation.aspx

Silverlight PowerShell Command Builder

We forgot all about this tool: a Silverlight app that allows you to build PowerShell commands for SharePoint 2010: http://www.microsoft.com/resources/TechNet/en-us/Office/media/WindowsPowerShell/WindowsPowerShellCommandBuilder.html It’s real easy to use and quite cool, although it actually goes against our principles. Trying to build PS cmds this way will actually prevent you from getting to learn PowerShell: not a good thing!