1

我有一个文档库,我们在其中添加了自定义文件夹内容类型,以便将文件夹所有者保留在自定义字段中。

现在我被要求将“添加新”表单的默认值设置为父文件夹所有者。我在下面尝试了此代码,但在保存新文件夹后会触发事件。有人可以帮我吗?如何在打开表单之前设置此默认值?

 public override void ItemAdding(SPItemEventProperties properties)
   {
       SPSecurity.RunWithElevatedPrivileges(delegate()
       {
           try
           {
               this.EventFiringEnabled = false;
               base.ItemAdding(properties);
               if (properties.List.RootFolder.Name == "Documents")
               {
                   SPWeb web = properties.List.ParentWeb;
                   SPList List = properties.List;
                   SPField fld = List.Fields["Folder Owner"];
                   SPUser usr = web.CurrentUser;
                   SPFieldUserValue curUser = new SPFieldUserValue();
                   curUser.LookupId = usr.ID;
                   SPFolder parentFolder = web.GetFolder(properties.AfterUrl.Substring(0,properties.AfterUrl.LastIndexOf("/")));
                   if (parentFolder.Item["Folder Owner"] == null)
                   {
                       fld.DefaultValue = curUser.ToString();
                   }
                   else
                   {
                       fld.DefaultValue = parentFolder.Item["Folder Owner"].ToString();
                   }
                   fld.Update();
                   List.Update();
               }
           }
           catch (Exception)
           {

           }

           finally
           {
               this.EventFiringEnabled = true;
           }

       });
   }
4

0 回答 0