在silverlight DataForm中添加新项和编辑项未正确更新SharePoint列表

时间:2011-07-11 15:05:09

标签: silverlight-4.0 sharepoint-2010 dataform

我正在尝试制作一个将在SharePoint网站上托管的简单Silverlight应用程序。 我正在从列表“testlist”中读取信息,我正在尝试使用数据表单控件来编辑,添加和删除列表中的数据。我能删除就好了。当我尝试添加它时,添加一个新条目,其中包含先前查看过的项目中的数据,我无法编辑当前项目。这是我的代码:

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)
                {
                    printers.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);
    }
}
}

和我的数据形式:

 <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"  />

感谢您的帮助。

更新:更改为这将在7个小时内回答它让我

1 个答案:

答案 0 :(得分:0)

没关系我弄明白我把我的编辑改为了这个并完全删除了我的addsNewitem方法。现在我只需要隐藏ID字段,它就会完美运行

 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);                
        }
    }