Thursday, January 24, 2013

Silverlight Datagrid bind with list items

This will be helpful for the beginners to learn how to bind the silverlight datagrid control with SharePoint List Items in a list. Create the List name called "Friends" and create column FirstName,LastName,Email. Create the the class "MyFriends" and the data members using getter and setter to map with the sharepoint list columns. While retrieving the collection of items we will add the generic class of the "MyFriends" class and subsequently binding them to Silverlight datagrid.
  public class MyFriends
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailID { get; set; }
            public int ItemID { get; set; }
        }

last member ItemID created for retrieving the item id for identify the each individual item from the collection of items for update and delete operation for further work.
        Web web;
        List list;
        ListItem item;
        ListItemCollection cols;
        private delegate void UpdateUI();


 public MainPage()
        {
            InitializeComponent();
            context = ClientContext.Current;
            web = context.Web;
            context.Load(web);
            list = web.Lists.GetByTitle("Friends");
            CamlQuery q = new CamlQuery();
            q.ViewXml = "";
            cols = list.GetItems(q);
            context.Load(cols);
            context.ExecuteQueryAsync(OnItemLoadSuccess, OnItemLoadFailed);
        }
        ClientContext context;
       
        private void OnItemLoadSuccess(object sender, ClientRequestSucceededEventArgs e)
        {
            UpdateUI _eventReg = BindItems;
            Dispatcher.BeginInvoke(_eventReg);
        }
        private void OnItemLoadFailed(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show("Error" + e.StackTrace.ToString());
        }
        public void BindItems()
        {
            List obj = new List();
            foreach (ListItem item in cols)
            {
                obj.Add(new MyFriends
                {
                    FirstName = item["First_x0020_Name"].ToString(),
                    LastName = item["First_x0020_Name"].ToString(),
                    EmailID = item["EmailID"].ToString(),
                    ItemID = Convert.ToInt32(item["ID"])
                });
                dataGrid1.ItemsSource = obj;
            }
        }

Tuesday, January 15, 2013

Adding new item silverlight with sharepoint

This code will help you to start the journey on How to develop SilverLight WebPart using SharePoint Client Side Object Model to add new item in SharePoint List.

Add the following sharepoint related assemblies which specifically tailored for the SilverLight and its Client side object model.

Microsoft.SharePoint.Client.SilverLight; Microsoft.SharePoint.Client.SilverLight.Runtime; These assemblies located at

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin
namespace SilverlightApplication7
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private delegate void UpdateUI();
        private ClientContext ctx;
        private Web web;
       
        List list;
     
        ListItem item;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ctx = ClientContext.Current;
            web = ctx.Web;
            ctx.Load(web);
            list = web.Lists.GetByTitle("Friends");
            ctx.Load(list);
            ListItemCreationInformation info = new ListItemCreationInformation();
            item = list.AddItem(info);
            item["Title"] = textBox1.Text;
            item["Name"] = textBox2.Text;
          
            item.Update();
            ctx.Load(item);
            ctx.ExecuteQueryAsync(OnOperationSuccess, OnOperationFailure);
           
        }
        public void ShowSuccessMessage()
        {
          
            MessageBox.Show("Added","Silverlight with SharePoint",MessageBoxButton.OK);
            textBox1.Text = "";
            textBox2.Text = "";
        }
        public void OnOperationSuccess(object sender, ClientRequestSucceededEventArgs e)
        {
            UpdateUI obj = ShowSuccessMessage;
            this.Dispatcher.BeginInvoke(obj);
        }
        public void OnOperationFailure(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show(e.Message.ToString());
        }
    }
}

Syntax HighLighter test



Murugesa Pandian.MCTS


Saturday, January 12, 2013

SharePoint 2010 Sandbox solution limitation

Below of these artifacts cannot be used in SharePoint 2010-Sandbox solution.

First thing is whatever the code which deals with outside of the server its not possible in Sandbox. Example: SPUtility.SendEmail method It needs to communicate with email server.

System.Net.Mail : This method can be used in classic asp.net web application or Farm solution in SharePoint but not in Sandbox solution. If you trying to send an email using this classes in your sandbox solution,It will compiles successfully but in run time it will throw SecurityException.

If you want to achieve this in sandbox solution then consider the Silverlight webpart with WCF or web service.

Next is the code which is trying to alter or create the pages in the file system in SharePoint Server is not allowed in Sandbox.

  • Application Pages
  • Mobile Pages
  • Site Definition and
  • UserControl(.ascx) - Visual WebPart.
  • Few tools available in CodePlex to create the Sandboxed visual webpart but it comes up with limitation that means some of the server object model and SharePoint controls cannot be used in it.

    Instead of using the visual webpart you can consider the ASP.NET WebPart in Sandbox solution.

    Finally,As soon as you select the solution type in Visual Studio for Sandbox solution VS will add the trimmed the features in Microsoft.SharePoint.dll.