Wednesday, November 9, 2011

Silverlight webpart in SharePoint 2010

Foolowing are the steps for creating a silver light webpart for SharePoint 2010.

1) Create a new project with the template "Silverlight Application"
2) Un-check "Host the Silverlight application in a new web site.
3)  Add the following references from this path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin".
Microsoft.SharePoint.Client.Silverlight.dll
Microsoft.SharePoint.Client.Silverlight.Runtime.dll

4) Add this reference as well System.Windows.Controls.Data.Input
5) After adding the reference, the solution pane should look like this:

6) Next step is to design the XAML:
< UserControl x:Class="SilverlightApplicationHelloWorld.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:datainput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" >
    < Grid x:Name="LayoutRoot" Background="White">
        < Button Click="btnLoadSite_Click" Content="Load Site" Height="23" HorizontalAlignment="Left" Margin="25,12,0,0" Name="btnLoadSite" VerticalAlignment="Top" Width="75" >< /Button >
        < Canvas Name="canvasLabels" Visibility="Collapsed" >         
            < datainput:Label Content="Site: " Height="22" HorizontalAlignment="Left" Margin="41,55,0,0" Name="label1" VerticalAlignment="Top" Width="73" >< /datainput:Label >
            < datainput:Label Height="22" HorizontalAlignment="Left" Margin="120,55,0,0" Name="label2" VerticalAlignment="Top" Width="233" >< /datainput:Label >
            < datainput:Label Content="Url:" Height="24" HorizontalAlignment="Left" Margin="41,84,0,0" Name="label3" VerticalAlignment="Top" Width="73" >< /datainput:Label >
            < datainput:Label Height="24" HorizontalAlignment="Left" Margin="120,84,0,0" Name="label4" VerticalAlignment="Top" Width="233" >< /datainput:Label >
            < datainput:Label Content="Description" Height="26" HorizontalAlignment="Left" Margin="41,116,0,0" Name="label5" VerticalAlignment="Top" Width="73" >< /datainput:Label >
            < datainput:Label Height="26" HorizontalAlignment="Left" Margin="120,116,0,0" Name="label6" VerticalAlignment="Top" Width="233" >
             < /datainput:Label >
        < /Canvas >
    < /Grid >
< /UserControl >

7) Add the following in code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
namespace SilverlightApplicationHelloWorld
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private ClientContext context = null;
        private Web web = null;
        private delegate void UpdateUIMethod();
        private void btnLoadSite_Click(object sender, RoutedEventArgs e)
        {
            context = ClientContext.Current;
            web = context.Web;
            context.Load(web, w => w.Title, w => w.Description, w => w.ServerRelativeUrl);
            context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);
        }
        private void OnSiteLoadSuccess(object sender, ClientRequestSucceededEventArgs e)
        {
            UpdateUIMethod updateUI = LoadSiteData;
            this.Dispatcher.BeginInvoke(updateUI);
        }
        private void OnSiteLoadFailure(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
        }
        private void LoadSiteData()
        {
            canvasLabels.Visibility = System.Windows.Visibility.Visible;
            label2.Content = web.Title;
            label4.Content = web.ServerRelativeUrl;
            label6.Content = web.Description;
        }
    }
}
8) For deployment there are two steps:
8a) Directly deploy to "Client Bin" folder.
Change the "Output Path" to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin" in the Solution -> Property page.
Now, add a Silverlight webpart on a sharepoint page and set the URL as
"_layouts/ClientBin/SilverlightApplicationHelloWorld.xap". 
 
8b) Other approach is add the XAP to a document libratry, and give the path in Silverlight webpart

Wednesday, November 2, 2011

Accessing Lists.asmx

I Got a requirement to access a list data through web service. Therefore, I consumed 'Lists.asmx' built-in service provided by SharePoint.


1) Following codes are required to get list item through CAML query

            Lists listService = new Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            listService.Url = "http://spsserver" + "/_vti_bin/lists.asmx";
            XmlDocument xmlDoc = new System.Xml.XmlDocument();
            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields =
                    xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptions =
                    xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");


            ndViewFields.InnerXml = "";
            ndQuery.InnerXml = "" +
                                "0";
            try
            {
                XmlNode ndListItems = listService.GetListItems("Tasks", null, ndQuery, ndViewFields,null, null, null);
                foreach (System.Xml.XmlNode listItem in ndListItems.ChildNodes[1].ChildNodes)
                {
                    if (listItem.OuterXml.Contains("ows_Title"))
                    {
                        txtValue.Text += Environment.NewLine + listItem.SelectSingleNode("@ows_Title").Value.Trim();
                        string strStartDate= listItem.SelectSingleNode("@ows_StartDate").Value.Trim();
                        DateTime dtConvert = DateTime.SpecifyKind(DateTime.Parse(strStartDate), DateTimeKind.Utc);
                        txtValue.Text += "  " + dtConvert.ToLocalTime().ToString();
                    }
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" +
                    ex.Detail.InnerText +"\nStackTrace:\n" + ex.StackTrace);
            }
2) This code is for updating a list item through batch update
            try

            {
                string strBatch = " 2" +
                                       "a test data";
                XmlDocument xmlDoc1 = new System.Xml.XmlDocument();
                System.Xml.XmlElement elBatch = xmlDoc1.CreateElement("Batch");
                elBatch.SetAttribute("OnError", "Continue");
                elBatch.SetAttribute("ListVersion", "1");
                elBatch.InnerXml = strBatch;
                XmlNode ndReturn = listService.UpdateListItems("Tasks", elBatch);
                MessageBox.Show(ndReturn.OuterXml);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

Saturday, September 24, 2011

Fixing issue "Access denied by Business Data Connectivity"

While working with BCS I have encountered an issue "Access denied by Business Data Connectivity"
Here, I was creating one external content type with a connection to a SQL server database. Please follow this blog to create such list.
After creating a SharePoint list, when I am trying to open it on the browser, following error is generated. 

 To Fix this issue, on the Central Administration, modify service application and grant permission to the BCS data connections. Please follow these steps: 




Now, the correct result is showing like this:

Wednesday, August 24, 2011

Delegate control in SharePoint

SharePoint offers a fine way to override it's built-in functionality. With the help of delegate control you do not required to modify the existing master page to modify existing features.

First, create a empty SharePoint project, and specify the target web url. Next select 'Farm Solution' a deployment option. Now add one User Control in the SharePoint mapped folder 'Control Templates'. Add functionality to this user control based on the requirements.
Now, create one element file, and add the following xml.
< ?xml version="1.0" encoding="utf-8"? >
< Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  < Control Id="GlobalNavigation"
           Sequence="10"
           ControlSrc="~/_ControlTemplates/SampleTestNavigation/TestNavigation.ascx"/ >
< /Elements >


Here, GlobalNavigation control defined in the master page will be overridden by this custom control. The Sequence value should be less than 100. 


Deploy the solution. And the output will be look like this:

Sunday, August 21, 2011

Creating UDC in InfoPath forms

During development there is common requirement is to create InfoPath forms, those can be deployed into multiple servers. Once you completed the development in local DEV, the forms need to deploy in Stage and Prod. Here an issue arises, the connection. Since the path of different servers are different one need to create 'Universal Data Connection' (UDC) 


1) Create a connection for submit data, and follow the wizard

2) Create a Connection Library, then click on the 'Convert' button, this will generate one XML file. Specify a name of the connection with extension UDCX

3) After creating the UDCX file, on the InfoPath form, add a new connection and browse all the connections on the server.

4) Important, for each server, Staging, Prod etc you have to create data connection file with the same name. In this example submit.udcx. The forms will pick dynamically the connection based the respective servers.




Friday, July 15, 2011

Sharepoint 2010 Timer Service error code 1069

On my local SharePoint server, Timer Service gives me an error "The service could not be started due to logon failure".


Initially I thought that, there is a need to enable some service from the Central Administration. Later realized that the issue is in the service itself.
The issue is fixed after setting of proper password within the service properties.


Thursday, July 7, 2011

Fixing error "Cannot generate serialization assembly"

Recently on my development machine I faced this error in Visual Studio. The error occurred at the build a project, and that project is already in use on the local machine.
Now, the problem here is, out the error message nothing clearly come out. After further research I found that the output dll is already present in GAC and after removing that dll, the project showed successful build.


Cheers!!
Somu