SharePoint:为什么我的字段的价值没有改变?

时间:2016-03-17 12:13:21

标签: sharepoint sharepoint-2013

SharePoint 2013

为什么我的领域的价值没有改变?

using Microsoft.SharePoint;
using System;
using System.IO;

namespace SharePoint2013_sandbox {
    internal static class Example {
        // I have some site for learning
        const String sitePath = "http://sharepoint2013/sites/sandbox2";
        // The Id of the field that is used by uploaded files
        static readonly Guid fieldId = new Guid(
            "{126801d9-c5c7-48a5-ab82-5ef48a76f934}");
        internal static void WorkWithFields() {

            if (!SPSite.Exists(new Uri(sitePath))) {
                Console.WriteLine("The {0} site not found.", sitePath);
            }
            else {
                using (SPSite siteCollection = new SPSite(sitePath)) {
                    using (SPWeb site = siteCollection.OpenWeb()) {

                        SPFieldChoice fldProjCode = site.Fields[fieldId]
                            as SPFieldChoice;

                        SPList list = site.Lists["Some document collection"];
                        using (FileStream fs = new FileStream(
                            @"C:\Public\Data\Drawing1.dwg",
                            FileMode.Open, FileAccess.Read, FileShare.Read)) {
                            SPFile file = list.RootFolder.Files.Add(
                                "Drawing1.dwg", fs, true);

                            // Now I set the value for the field of the file...
                            String fldValue = fldProjCode.Choices[1];
                            file.Item[fldProjCode.Id] = fldValue;

                            file.Update();
                        }
                        site.Update(); // But field value of my file wasn't updated.
                    }
                }
            }
            Console.WriteLine("Press any key for exit...");
            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在file.ListItemAllFields.Update();行之前添加了file.Update();代码行,现在该字段也已更新。