Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, March 29, 2013

Delete a SharePoint ListItem using silverLight Client Object Model



Write this in button click event.
private string siteUrl = "http://Spserver:100/";

try
            {
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("New Ideas");
                ListItem oListItem = oList.GetItemById(4);
                oListItem.DeleteObject();
                clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            }
            catch (Exception Ex)
            {
                lblErrorMessage.Content = Ex.ToString();
            }

private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
        {
            UpdateUIMethod updateUI = DisplayInfo;
            this.Dispatcher.BeginInvoke(updateUI);
        }

        private void DisplayInfo()
        {
            //MyOutput.Text = "New item created in " + oList.Title;
            lblErrorMessage.Content = "Item Deleted";
        }

        private delegate void UpdateUIMethod();

        private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
        {
            MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
        }

Update a SharePoint ListItem using silverLight Client Object Model



Write this in button click event.
private string siteUrl = "http://SpServer:100/";

try
            {
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("New Ideas");
                ListItem oListItem = oList.GetItemById(3);

                oListItem["Title"] = txtNewIdea.Text;

                oListItem.Update();
                clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            }
            catch (Exception Ex)
            {
                lblErrorMessage.Content = Ex.ToString();
            }
private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
        {
            UpdateUIMethod updateUI = DisplayInfo;
            this.Dispatcher.BeginInvoke(updateUI);
        }

        private void DisplayInfo()
        {
            //MyOutput.Text = "New item created in " + oList.Title;
            lblErrorMessage.Content = "Item Updated";
        }

        private delegate void UpdateUIMethod();

        private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
        {
            MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
        }

Thursday, March 28, 2013

Insertion into a SharePoint List using SilverLight Client Object Model.


Ascx
<UserControl x:Class="InsertApp_Silverlight.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">

    <Grid x:Name="LayoutRoot" Background="White">
        <dataInput:Label Height="19" HorizontalAlignment="Left" Margin="106,30,0,0" Name="lblHeading" VerticalAlignment="Top" Width="176" Content="Insertion Sample into SharePoint List using SilverLight Client Object Model" />
        <dataInput:Label Height="19" HorizontalAlignment="Left" Margin="74,65,0,0" Name="lblTitle" VerticalAlignment="Top" Width="94" Content="Enter Title" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="141,63,0,0" Name="txtTitle" VerticalAlignment="Top" Width="120" />
        <Button Content="Save Record" Height="23" HorizontalAlignment="Left" Margin="117,108,0,0" Name="btnSave" VerticalAlignment="Top" Width="118" Click="btnSave_Click" />
    </Grid>
</UserControl>
Ascx.Cs
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 InsertApp_Silverlight
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClientContext context = ClientContext.Current;
                Web currentWeb = context.Web;
                List lst = currentWeb.Lists.GetByTitle("New Ideas");
                ListItem item = lst.AddItem(new ListItemCreationInformation());
                item["Title"] = txtTitle.Text;
                item.Update();
                context.Load(lst, NewIdeasList => NewIdeasList.Title);
                context.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
            }
        }

        private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
        {
            UpdateUIMethod updateUI = DisplayInfo;
            this.Dispatcher.BeginInvoke(updateUI);
        }

        private void DisplayInfo()
        {
            MessageBox.Show("Item Inserted Successfully");
            txtTitle.Text = "";
        }

        private delegate void UpdateUIMethod();

        private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
        {
            MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
        }
    }
}