【问题标题】:oData SharePoint Add List Item Containing Value PropertiesoData SharePoint 添加包含值属性的列表项
【发布时间】:2015-06-16 21:03:24
【问题描述】:

如何保存属于另一个 SharePoint 列表项的“Value”和“DataServiceCollection”对象?这些是我的模型中唯一没有保存的属性。

生成的 Food SharePoint 模型具有以下属性:

public class Food
{
    DataServiceCollection<FoodIngredientValue> Ingredient;
    FoodStateValue State;
    string _StateValue
}

首先,我不知道为什么有两种方法可以在 SharePoint 生成的模型中添加状态值。我尝试填充其中一个,但状态值不会填充到 SharePoint 中。

其次,我尝试通过在保存之前将 FoodIngredientValue 对象硬编码到食物模型来填充成分集合,并且还通过查询 SharePoint 并将它们分配给成分属性,但它没有保存在 SharePoint 中。

我使用下面的代码向 SharePoint 列表中添加了一个新的食物项目,并且我验证了所有三个属性都已填充到我的模型中,但没有一个被保存。

public bool Insert(Food food)
{
    var dataContext = new FoodDataContext(new Uri(EndpointUrl)) { Credentials = CredentialCache.DefaultNetworkCredentials };
    dataContext.AddToFoods(food);
    var response = dataContext.SaveChanges().FirstOrDefault();
    return response.StatusCode == 201;
}

【问题讨论】:

    标签: c# sharepoint sharepoint-2010 odata wcf-data-services


    【解决方案1】:

    这是一篇很棒的博文,解释了如何在 SharePoint oData API 中链接复杂的列表项(DataServiceCollecton 和 Value 对象):

    http://blog.heeresonline.com/2014/07/sharepoint-wcf-dataservices-choice-lookup-column-editing/

    要记住的重要一点是,在开始填充 DataServiceCollection 或 Value 对象类型的复杂字段之前,将新项目添加到数据上下文中。对于 DataServiceCollection 类型的属性,需要做更多的工作才能正确链接它们,以便将它们保存在数据上下文中,如下面的成分所示。以下是最终成功的代码示例:

    var foodItem = new FoodItem();
    dataContext.AddToFoods(foodItem);   // Add to context before populating fields so the values are tracked.
    foodItem = Mapper.Map(newFood, foodItem);
    
    // DataValue Properties like StateValue objects can now be added since it is tracked by the context.
    var state = StateValue.CreateStateValue("Montana");
    foodItem.StateValue = state;
    
    // Need to link special DataServiceCollection lists like Ingredient using a reference.
    if (newFood.Ingredient != null)
    {
        newFood.Ingredient.ForEach(c =>
        {
            var ingredient = FoodIngredient.CreateFoodIngredientValue(c);
            dataContext.AttachTo("FoodIngredientValue", ingredient);
            foodItem.FoodIngredient.Add(ingredient);
            dataContext.AddLink(foodItem, "FoodIngredient", ingredient);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多