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