Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

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:

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.


Monday, February 28, 2011

File Not Found exception on SharePoint 2010

There is a common practice of SharePoint Development is to create a windows or console application to fetch several SharePoint data. This method is widely used because there is no deployment steps to perform. The only limitation of using such application is, you have be on the SharePoint Server.


While trying to access to list information of a SharePoint 2010 site, I used a windows application to fetch data. To my surprise, I am getting a "File not found" exception. But the same code working perfectly fine in MOSS 2007


It's a very wired message, as I am not trying any file operation. And the message forced me to searched on the web several hours. And finally understood that the issue is in the build configuration. 
By default the compilation is in 32 bit (86x), this need to be changed to "Any CPU".
That's all. After this changes my code is working fine.


Friday, November 12, 2010

Creating a SandBox solution in SharePoint 2010

The Sandbox solution is newly added in SharePoint 2010, and it is very useful particularly for the Administrators to track each of the custom development object. 


Now, the administrator can monitor the performance of the custom application and that can be removed if more server resources are consumed. Another important thing is, the Sandbox solution run on a separate process thread SPUserCodes. This will stop taking SharePoint resources already using W3WP process.


For every site, there is a solution gallery, where already installed sandbox solutions are present.













Steps for creating the solution:


1) Create a blank solution in VS 2010, and specify the site URL and select "Sandbox solution"


2) Add a webpart, by click on project -> New Items
Please Note, the Visual Webpart is not supported in a Sandbox solution.


3) Write you custom codes for your webpart in the "CreateChildControl" method.


4) Build the solution and locate the WSP file in the Bin folder.


5) Now, open Power Shell and execute these commands for adding and installing the solution.


Add-SPUserSolution -LiteralPath "Physical path to WSP" -Site "Your site Url"


Install-SPUserSolution -Identity "WSP name" -Site "Your site url"
































Remember: Run the Power Shell as an administrator.