Monday, June 13, 2011

SharePoint 2010 - Set default value for a lookup column using InfoPath and no code

If you have a look up column in your SharePoint 2010 list and want to assign a default value while adding an item (insert mode) then this post is relevant for you. Read on.

What you need to know before proceeding further:
1) You need to have InfoPath installed on your machine.
2) You need to know how you can use infopath to create custom forms for adding or editing items in the list.

See this video on how to Customize a SharePoint List Form using InfoPath 2010


Follow these steps:
1) Go to the SharePoint list and click on Customize form(under the List tab)


2) Clicking on "Customize form" will open up the InfoPath with list items (uses default page layout).

3) Click on the lookup field from the Fields list and then properties. Set the default value of the field in the default value section.



4) Make sure you have at least one item in your lookup list.

5) Enter a value in the default value text box (numeric id not text) of the default value section and click OK.

6) Publish the InfoPath by clicking on the quick publish button (on the top left corner beside save button).

You are done. You should be able to see the default value when you try to add an item to the SharePoint list.

Wednesday, June 1, 2011

Sharepoint 2010 error while deploying solution - This solution contains no resources scoped for a web application

Error:
This solution contains no resources scoped for a web application and cannot be deployed to a particular web application

Fix that worked for me:
stsadm and powershell commands were giving me the above error and I went to central admin site and manually deployed the solution.
Go to Central Admin -> System Settings -> Farm Management -> Manage Farm solutions -> Select the solution you want to deploy and finally deploy

Friday, April 29, 2011

date converter for displaying a short date string from a date object

We (WPF/Silverlight developers) typically use date converter in almost every WPF or Silverlight app. Below is the code for converting date time object to a short date string.

C# code:


[ValueConversion(typeof(DateTime), typeof(String))]
    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToShortDateString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return DependencyProperty.UnsetValue;
        }
    }

VB.net code:

<ValueConversion(GetType(DateTime), GetType([String]))> _
Public Class DateConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
Dim [date] As DateTime = DirectCast(value, DateTime)
Return [date].ToShortDateString()
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
Dim strValue As String = TryCast(value, String)
Dim resultDateTime As DateTime
If DateTime.TryParse(strValue, resultDateTime) Then
Return resultDateTime
End If
Return DependencyProperty.UnsetValue
End Function
End Class

XAML code for displaying "Star" shape

 <Polygon  Fill="Yellow" Stroke="Black" StrokeThickness=".5" StrokeLineJoin="Round" Width="15" Height="15" Stretch="Fill" Points="10,2 10,7 17,7 12,10 14,15 9,12 4,15 6,10 1,7 7,7" Name="star"/>

Tuesday, February 1, 2011

Extension method in VB.net to clear items in a SharePoint List

        ''' <summary>
        ''' Clear all items in a SharePoint list - Created by Ken
        ''' </summary>
        ''' <param name="list"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub Clear(ByRef list As SPList)
            If Not list.ItemCount = 0 Then
                Dim sbDelete As New System.Text.StringBuilder
                sbDelete.Append("<?xml version=""1.0"" encoding=""UTF-8""?><Batch>")
                For Each listItem In list.Items
                    sbDelete.Append("<Method>")
                    sbDelete.Append("<SetList>" & list.ID.ToString & "</SetList>")
                    sbDelete.Append("<SetVar Name=""Cmd"">DELETE</SetVar>")
                    sbDelete.Append("<SetVar Name=""ID"">" + listItem("ID").ToString + "</SetVar>")
                    sbDelete.Append("</Method>")
                Next
                sbDelete.Append("</Batch>")
                list.ParentWeb.ProcessBatchData(sbDelete.ToString())
                list.Update()
            End If
        End Sub

Another implementation of the same is shown below. Performance is very poor with the below code. If you have a lot of list items then make sure you use processbtachdata instead.


        ''' <summary>
        ''' Clear all items in a SharePoint list - Created by Ken
        ''' </summary>
        ''' <param name="list"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub Clear(ByRef list As SPList)
            If Not list.ItemCount = 0 Then
                For i = list.ItemCount - 1 To 0 Step -1
                    Dim listItem = list.Items(i)
                    listItem.Delete()
                Next
                list.Update()
            End If
        End Sub


In VB 10 you do not need the underscore after the attribute declaration. In other words, line continuation is supported in VB 10 / Visual studio 2010/ .net framework 4.0. For more info refer to Scott guthries article 

Similar example can be found here

Friday, October 29, 2010

Service provider is missing the INameResolver service - WPF error

I got this error when I tried to use the new WPF 4 x:reference feature in visual studio 2010. Apparently, it is one of the VS 2010 XAML designer's glitches. Stop banging your head on the desk trying to figure out what went wrong and ignore this error.

Here is the explanation from Adam Nathan's WPF 4 unleashed book:
"The x:Reference markup extension is often mistakenly associated with the XAML2009
features that can only be used from loose XAML at the time of this writing. Although
x:Reference is a new feature in WPF 4, it can be used from XAML2006 just fine as long as your project is targeting version 4 or later of the .NET Framework.
One glitch is that the XAML designer in Visual Studio 2010 doesn’t properly handle x:Reference, so it gives the following design-time error that you can safely ignore:
Service provider is missing the INameResolver service"





Tuesday, October 26, 2010

C# Object vs Var vs Dynamic primitive data types

using System;
using System.Linq;

// Object Vs Var Vs Dynamic keywords in C#
class Program
{
public static void Main()
{
// Object
object objWhat = new Char[] { 'k', 'u', 'm', 'a', 'r' };
Console.WriteLine((char[])(objWhat)); // Strongly typed

// Var
var what = new Char[] { 'k', 'i', 'r', 'a', 'n' };
Console.WriteLine(what); // Strongly typed

// Dynamic
for (Int32 demo = 0; demo < 2; demo++)
{
var arg = (demo == 0) ? (dynamic)5 : (dynamic)"A"; // not known till tun time - weakly typed
var result = Plus(arg);
M(result);
}

Console.ReadLine();
}

private static dynamic Plus(dynamic arg) { return arg + arg; } // dynamic method
private static void M(Int32 n) { Console.WriteLine("M(Int32): " + n); }
private static void M(String s) { Console.WriteLine("M(String): " + s); }
}

// Output looks like this
// kumar
// kiran
// M(Int32): 10
// M(String): AA

// dynamic code taken from: Jeffrey's book CLR via C#