开发者

Add new Item and Edit Item in silverlight DataForm not correctly updating SharePoint List

I am trying to make a simple Silverlight application that will be hosted on a SharePoint site. I am reading the information from the list "testlist" and I am trying to use a dataform control to edit, add and delete data from the list. I am able to delete just fine. When I try to add it adds a new entry with the data from the item previously viewed and I am unable to edit current Items whatsoever. Here is my code:

namespace SP2010
{
public partial class MainPage : UserControl
{
    ClientContext context;
    List customerList;
    ListItemCollection allCustomers;
    public MainPage()
    {
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        InitializeComponent();
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;
        context = new ClientContext(ApplicationContext.Current.Url);
        customerList = context.Web.Lists.GetByTitle("testlist");
        context.Load(customerList);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml =@"<View><Query></Query><RowLimit>1000</RowLimit></View>";
        allCustomers = customerList.GetItems(camlQuery);
        context.Load(allCustomers);
        context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));
    }
    private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
    {
        Dispatcher.BeginInvoke(DisplayListData);
    }
    private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args)
    {
        MessageBox.Show(args.ErrorDetails + "   " + args.Message);
    }
    public ObservableCollection<SPCustomers> Printers
    {
        get
        {
            if (printers == null)
            {
                printers = new ObservableCollection<SPCustomers>();
                foreach (ListItem item in allCustomers)
                {
                    p开发者_Go百科rinters.Add(new SPCustomers
                    {
                        IPAddress = item["Title"].ToString(),
                        Make = item["make"].ToString(),
                        Model = item["model"].ToString(),
                        xCord = item["x"].ToString(),
                        yCord = item["y"].ToString(),
                        id = Convert.ToInt32(item["ID"].ToString())
                    });
                }
            }
            return (printers);
        }
    }
    private ObservableCollection<SPCustomers> printers;
    private void DisplayListData()
    {
        dataForm1.DataContext = Printers;
    }
    private void dataForm1_EditEnded(object sender, DataFormEditEndedEventArgs e)
    {
        if (e.EditAction == DataFormEditAction.Commit)
        {
            if (dataForm1.IsItemChanged)
            {
                customerList = context.Web.Lists.GetByTitle("testlist");
                SPCustomers printer = dataForm1.CurrentItem as SPCustomers;
                ListItem items = customerList.GetItemById(printer.id);
                items["Title"] = printer.IPAddress;
                items["make"] = printer.Make;
                items["model"] = printer.Model;
                items["x"] = printer.xCord;
                items["y"] = printer.yCord;

                items.Update();

                context.ExecuteQueryAsync(OnRequestSucceeded, OnRequestFailed);
            }
        }
    }

    private void dataForm1_AddingNewItem(object sender, DataFormAddingNewItemEventArgs e)
    {
        customerList = context.Web.Lists.GetByTitle("testlist");
        SPCustomers printe = dataForm1.CurrentItem as SPCustomers;
        ListItemCreationInformation itemcreationinfo = new ListItemCreationInformation();
        ListItem items = customerList.AddItem(itemcreationinfo);
        items["Title"] = printe.IPAddress;
        items["make"] = printe.Make;
        items["model"] = printe.Model;
        items["x"] = printe.xCord;
        items["y"] = printe.yCord;

        items.Update();

        context.ExecuteQueryAsync(OnRequestSucceeded, OnRequestFailed);
    }

    private void dataForm1_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e)
    {
        SPCustomers printer = dataForm1.CurrentItem as SPCustomers;
        ListItem oListItem = customerList.GetItemById(printer.id);
        oListItem.DeleteObject();
        context.ExecuteQueryAsync(OnRequestSucceeded, OnRequestFailed);
    }
}
}

and my dataform:

 <df:DataForm ItemsSource="{Binding}"  Height="276" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataForm1" VerticalAlignment="Top" Width="376" EditEnded="dataForm1_EditEnded" AddingNewItem="dataForm1_AddingNewItem" DeletingItem="dataForm1_DeletingItem" CommandButtonsVisibility="All"  />

Thanks for the help.

update: changed to this will answer in like 7 hours when it lets me


never mind I figured it out I changed my editended to this and deleted my addingNewitem method entirely. Now I just have to hide the ID field and it will be working perfectly

 private void dataForm1_EditEnded(object sender, DataFormEditEndedEventArgs e)
    {
        if (e.EditAction == DataFormEditAction.Commit)
        {
            customerList = context.Web.Lists.GetByTitle("testlist");
            SPCustomers printer = dataForm1.CurrentItem as SPCustomers;
            ListItem items;
            if (printer.id != 0)
            {
                items = customerList.GetItemById(printer.id);
            }
            else
            {
                ListItemCreationInformation itemcreationinfo = new ListItemCreationInformation();
                items = customerList.AddItem(itemcreationinfo);
            }
                items["Title"] = printer.IPAddress;
                items["make"] = printer.Make;
                items["model"] = printer.Model;
                items["x"] = printer.xCord;
                items["y"] = printer.yCord;
                items.Update();

                context.ExecuteQueryAsync(OnRequestSucceeded, OnRequestFailed);                
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜