Wednesday, April 18, 2012

Migrating SharePoint list from 2007 to SharePoint 2010

Recently I came across a requirement of migrating a List from SharePoint 2007 to SharePoint 2010.If you Google most of the suggestions are using tools like metalogix,Quest migration manager,DocAve.
We all know that we can save a list as template and upload in SharePoint 2010,but remember using .stp files are not supported in SharePoint 2010.
So this is the article which made life easy.

Monday, October 24, 2011

How to fix: Recurring Meeting Workspace error

I recently developed a Custom Master Page.Every thing worked fine but  When I selected the custom master page for Meeting workspace it started throwing a java script error ‘g_InstanceID’ is undefined.
After some digging back I came to know that the default master page for meeting Workspace is MWSDefault.master which contains the PropertyBag web control.
Started searching and found the solution here.

Thursday, September 22, 2011

Iterating against Multiline Field

We all know how to iterate on single line of text field in SharePoint.But its a bit tricky when it comes to Multiline Text field.I think its more hassle than Lockup column.
Here is how we get the data from the Field.
SPListItemCollection itemCol = List.GetItems(query);
 SPListItem item = itemCol[i];
SPFieldMultiLineText myField =item.Fields.GetField("fieldName") as SPFieldMultiLineText;
string myString=myField.GetFieldValueAsText(item["fieldName"]);

Even though the string gets with \r\n in order to diplay on console here is how to code
string temp = item["myField"].ToString();
string[] fieldValue = temp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
The above method helps in removing the \r and \n present.
If you are show on web .use a textbox and set the textmode property to "multiline".This will automatically display in same format present as in SharePoint List.
Ex:<asp:TextBox ID="txtField" runat="server" TextMode="MultiLine"></asp:TextBox>
txtField.Text=myField.GetFieldValueAsText(item["fieldName"]);

Tuesday, August 30, 2011

Using CAML to get items based on DateTime

Most of the Times we work on retrieve items from List or Document Library based on different Columns.This article is to target the datetime column.

Scenario 1: I want to get items which are greater than or equal to Today's date
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today /></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The attribute "Today" helps in defining the date to be Current Date
Scenario 2: We Want to get result from say 5 days from Today
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today  Offset="5"/></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The Attribute "Offset" defines the No.of days we want to go back




Tuesday, August 16, 2011

Calling List webservice using Jquery

Most of the times we generally use object model to query SharePoint List.But not all development environments gives you an option of custom code.So JQuery along with Content Editor WebPart.
Here is the scenario I had a case of using dataview webpart which needs to be filtered basing on criteria, as we can create a parameter based on query string.In this case I am intrested in the filter rather than how to create DVWP.So coming to the coding part
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<getlistitems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listname>AppStatusList</listName> \
<viewfields> \
<viewfields> \
<fieldref Name='Title' /> \
</ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";

$.ajax({
url: "http://balaapd004/dept/cs/test/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});

$('#ddlSelect').change(function() {
//var selectedValue=
$('#txtResult').val($('#ddlSelect option:selected').text());
});
});

function processResult(xData, status) {
var listItems="";

$(xData.responseXML).find("z\\:row").each(function() {
listItems+= "<option value='" + $(this).attr("ows_ID") + "'>" + $(this).attr("ows_Title") + "</option>";
$("#ddlSelect").html(listItems);

});
}
</script>

<select id="ddlSelect">
</select>
<input type="text" id="txtResult" />

Monday, August 1, 2011

Extension Method to iterate against any control on a Page

We all know that in order to iterate against any control on page a method we generally go for is
Page.FindControl.
But this is a bit tedious process when we associate the same page with Master Page which adds one more foreach loop on the top and the best vs bad part is it gives you a bit of debugging time.

I think the best way of doing this is using the Extensions method which comes with c# 3.0

public static class myExtensions
        {        
            public static IEnumerable<T> AllControls<T>(this Control ctrl) where T : Control
            {
                bool find = ctlr is T;
                if (find)
                {
                    yield return ctrl as T;
                }
                foreach (var child in ctrl.Controls.Cast<Control>())
                {
                    foreach (var item in AllControls<T>(child))
                    {
                        yield return item;
                    }
                }
            }
        }

This gives you an ease in different ways as it can used along with Lambda expressions like this
Page.AllControls<CheckBox>().Where(c => c.Checked);

or simply to iterate on the control you want to find
 var chkBox = Page.AllControls<CheckBox>();

foreach (CheckBox i in chkBox)
       {
    //Your code
       }

Thursday, July 28, 2011

Passing Property Value from WebPart to USer Control in MOSS

Any time came across a scenario where you have to pass property value from WebPart hosting the UserControl to pass value in SharePoint 2007.
Here is a small example how you can achive this.
The Solution structure
As you see by default you can't see the User Control template ,the easiest way of  doing this Unload the project "Right Click"  click as shown in the image
Add this GUID under the section where it says <ProjectTypeGuids>
{349C5851-65DF-11DA-9384-00065B846F21};
Save it and reload the project "Bingo" you can see the User Control template.
WebPart Code :
 public class ValueToUC : System.Web.UI.WebControls.WebParts.WebPart
    {
        private bool _error = false;
        private string _myName = null;
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ValueToUC/ValueUC.ascx";
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Properties")]
        [WebDisplayName("MyName")]
        [WebDescription("MyName")]
        public string MyProperty
        {
            get { return _myName;  }
            set { _myName = value; }
        }
     
        public ValueToUC()
        {
            this.ExportMode = WebPartExportMode.All;
            this.ChromeType = PartChromeType.None;
        }     

        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();

                    ValueUC uc = (ValueUC)Page.LoadControl(_ascxPath);
                    uc.parentWp = this;
                    this.Controls.Add(uc);                                                     
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }
     
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }

User Control Code:
public partial class ValueUC : System.Web.UI.UserControl
    {
        public ValueToUC parentWp
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblName.Text = parentWp.MyProperty;
        }
    }
As you observe I am using the WebPart class as a property which will help in getting the value to the User Control.