public static string SaveFieldItems(string guid, List <SPFieldValue> fields, List <SPListItemDelete> deletedItems, List <Attachment> addFiles, List <Attachment> deleteFiles)
        {
            List <ErrorMessage> list = new List <ErrorMessage>();
            string s = "ok";

            List <SPItemSave> saveItems  = new List <SPItemSave>();
            SPWeb             web        = SPContext.Current.Web;
            SPList            masterList = web.Lists[new Guid(guid)];
            string            url        = masterList.RootFolder.Url;
            string            listname   = url.Substring(url.LastIndexOf('/') + 1);

            foreach (SPListItemDelete delete in deletedItems)
            {
                SPList delList = web.Lists[new Guid(delete.ListId)];
                s = Utility.DeleteItemFromList(delList, delete.ItemId);
                if (s != "ok")
                {
                    return(s);
                }
            }
            SPFieldValue idValue = fields.FirstOrDefault <SPFieldValue>(a => a.InternalName == "ID");
            SPListItem   item    = (idValue.value != "0") ? masterList.GetItemById(int.Parse(idValue.value)) : null;



            if (s == "ok")
            {
                List <SPFieldValue> masterDetailFields = fields.Where(a => a.Type == "MasterDetail").ToList();
                // SPListItemCollection items = Utility.FindValidation(web, list4.ID);



                int result = 0;
                s = Utility.UpdateFiles(web, "", fields, addFiles, deleteFiles);
                if (s != "ok")
                {
                    return(s);
                }
                SPListItem itemSave = (int.Parse(idValue.value) > 0) ? masterList.GetItemById(int.Parse(idValue.value)) : masterList.AddItem(masterList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.File);
                SPListItem itemSP   = setItemFields(fields, itemSave);
                SPItemSave save     = new SPItemSave
                {
                    ListId = guid,
                    Item   = itemSP,
                    Folder = web.GetFolder(masterList.RootFolder.ServerRelativeUrl)
                };
                s = SaveItem(web, save.ListId, save.Item, save.Folder, "");
                int.TryParse(s, out result);
                if (result > 0)
                {
                    s = "ok";
                    using (List <SPFieldValue> .Enumerator masterDetaileEnum = masterDetailFields.GetEnumerator())
                    {
                        while (masterDetaileEnum.MoveNext())
                        {
                            SPFieldValue detailField = masterDetaileEnum.Current;
                            SPFieldValue lk          = new SPFieldValue
                            {
                                InternalName = detailField.value,
                                Type         = "Lookup",
                                value        = result.ToString()
                            };
                            Guid siteID = web.Site.ID;
                            SPSecurity.RunWithElevatedPrivileges(delegate
                            {
                                using (SPSite site = new SPSite(siteID))
                                {
                                    using (SPWeb web1 = site.OpenWeb())
                                    {
                                        SPList detailList = web1.Lists[new Guid(detailField.LookupList)];
                                        //SPListItemCollection detailItems = Utility.FindValidation(web, detailList.ID);
                                        SPFolder folder = web1.GetFolder(detailList.RootFolder.ServerRelativeUrl);
                                        foreach (List <SPFieldValue> details in detailField.rows)
                                        {
                                            details.Add(lk);
                                            SPFieldValue detailIdValue = details.FirstOrDefault <SPFieldValue>(a => a.InternalName == "ID");
                                            SPListItem detailItem      = setItemFields(details, (int.Parse(detailIdValue.value) > 0) ? detailList.GetItemById(int.Parse(detailIdValue.value)) : detailList.AddItem(folder.Url, SPFileSystemObjectType.File));
                                            SPItemSave detailsave      = new SPItemSave
                                            {
                                                ListId = detailField.LookupList,
                                                Item   = detailItem,
                                                Folder = folder
                                            };
                                            saveItems.Add(detailsave);
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                string msgResult = "";
                int    num10     = 0;
                foreach (SPItemSave saveItem in saveItems)
                {
                    msgResult = SaveItem(web, saveItem.ListId, saveItem.Item, saveItem.Folder, "");
                }
                int.TryParse(msgResult, out num10);
            }
            return(s);
        }
        private static void SetFieldValue(SPListItem item, SPFieldValue fieldValue)
        {
            int num;

            switch (fieldValue.Type)
            {
            case "Text":
                item[fieldValue.InternalName] = fieldValue.value;
                break;

            case "Note":
                item[fieldValue.InternalName] = fieldValue.value;
                break;

            case "Number":
                item[fieldValue.InternalName] = decimal.Parse(fieldValue.value);
                break;

            case "DateTime":
                item[fieldValue.InternalName] = Convert.ToDateTime(fieldValue.value);
                break;

            case "Lookup":
                item[fieldValue.InternalName] = new SPFieldLookupValue(int.Parse(fieldValue.value), "");
                break;

            case "LookupMulti":
            {
                string[] strArray = fieldValue.value.Split(new char[] { ',' });
                SPFieldLookupValueCollection values = new SPFieldLookupValueCollection();
                num = 0;
                while (num < strArray.Length)
                {
                    values.Add(new SPFieldLookupValue(int.Parse(strArray[num]), ""));
                    num++;
                }
                item[fieldValue.InternalName] = values;
                break;
            }

            case "RelatedCustomLookupQuery":
                item[fieldValue.InternalName] = new SPFieldLookupValue(int.Parse(fieldValue.value), "");
                break;

            case "CustomComputedField":
                item[fieldValue.InternalName] = new SPFieldLookupValue(int.Parse(fieldValue.value), "");
                break;

            case "Choice":
                item[fieldValue.InternalName] = fieldValue.value;
                break;

            case "MultiChoice":
            {
                string[] strArray2             = fieldValue.value.Split(new char[] { ',' });
                SPFieldMultiChoiceValue value2 = new SPFieldMultiChoiceValue();
                for (num = 0; num < strArray2.Length; num++)
                {
                    value2.Add(strArray2[num]);
                }
                item[fieldValue.InternalName] = value2;
                break;
            }

            case "Boolean":
                item[fieldValue.InternalName] = fieldValue.value;
                break;
            }
        }
Пример #3
0
        public static string SaveFieldItems(string guid, List <SPFieldValue> fields, List <SPListItemDelete> deletedItems, List <Attachment> addFiles, List <Attachment> deleteFiles)
        {
            List <ErrorMessage> list = new List <ErrorMessage>();
            string s             = "ok";
            string sourceValue   = "";
            string destValue     = "";
            string compareAction = "";

            string[]          sourceFieldArray = null;
            List <int>        viewersIds       = new List <int>();
            List <int>        editorsIds       = new List <int>();
            List <SPItemSave> saveItems        = new List <SPItemSave>();
            int    creatorId  = 0;
            int    num2       = 0;
            int    num3       = 0;
            int    num4       = 0;
            int    num5       = 0;
            int    num6       = 0;
            int    contractId = 0;
            SPWeb  web        = SPContext.Current.Web;
            SPList list4      = web.Lists[new Guid(guid)];
            string url        = list4.RootFolder.Url;
            string listname   = url.Substring(url.LastIndexOf('/') + 1);

            foreach (SPListItemDelete delete in deletedItems)
            {
                SPList list5 = web.Lists[new Guid(delete.ListId)];
                s = Utility.DeleteItemFromList(list5, delete.ItemId);
                if (s != "ok")
                {
                    return(s);
                }
            }
            SPFieldValue value2          = fields.FirstOrDefault <SPFieldValue>(a => a.InternalName == "ID");
            SPListItem   item            = (value2.value != "0") ? list4.GetItemById(int.Parse(value2.value)) : null;
            string       permissionField = Utility.GetPermissionFieldLookup(web, guid);
            SPFieldValue value3          = fields.FirstOrDefault <SPFieldValue>(a => a.InternalName == permissionField);
            SPFieldValue value4          = fields.FirstOrDefault <SPFieldValue>(a => a.InternalName == "Area");

            Utility.GetContractUsers(web, guid, (value3 != null) ? int.Parse(value3.value) : 0, (value4 != null) ? int.Parse(value4.value) : 0, out contractId, out creatorId, out num2, out num3, out num4, out num5, out num6);
            if ((value2.value == "0") || ((value2.value != "0") && (new SPFieldLookupValue(item["CurrentUser"].ToString()).LookupId == web.CurrentUser.ID)))
            {
                SPFieldValue value5 = new SPFieldValue {
                    InternalName = "Status",
                    Type         = "Text",
                    value        = (num2 > 0) ? "در انتظار تایید" : "پایان فرآیند"
                };
                fields.Add(value5);
                SPFieldValue value6 = new SPFieldValue {
                    InternalName = "CurrentUser",
                    Type         = "User",
                    value        = (num2 > 0) ? num2.ToString() : "0"
                };
                fields.Add(value6);
            }
            GetItemPermissionFromContract(web, list4, (value3 != null) ? int.Parse(value3.value) : 0, (value4 != null) ? int.Parse(value4.value) : 0, web.CurrentUser.ID, out viewersIds, out editorsIds);
            if (s == "ok")
            {
                List <SPFieldValue> list6 = (from a in fields
                                             where a.Type == "MasterDetail"
                                             select a).ToList <SPFieldValue>();
                SPListItemCollection items = Utility.FindValidation(web, list4.ID);



                foreach (SPListItem item2 in items)
                {
                    List <string>       fieldNames = item2["SourceField"].ToString().Split(',').ToList();
                    List <SPFieldValue> checkField = fields.Where(a => fieldNames.Any(b => b == a.InternalName)).ToList();
                    if (checkField.Count > 0)
                    {
                        Utility.CheckValidation(web, item2, fields, out sourceFieldArray, out sourceValue, out destValue, out compareAction);
                        if (((destValue != "") && (sourceValue != "")) && !Utility.CompareCheck(compareAction, sourceValue, destValue))
                        {
                            ErrorMessage message = new ErrorMessage {
                                FieldNames = fieldNames,
                                Message    = item2["Message"].ToString(),
                                RowNumber  = -1
                            };
                            list.Add(message);
                        }
                    }
                }
                foreach (SPFieldValue value7 in list6)
                {
                    SPList list8 = web.Lists[new Guid(value7.LookupList)];
                    SPListItemCollection items2 = Utility.FindValidation(web, list8.ID);
                    int num7 = 0;
                    foreach (List <SPFieldValue> list9 in value7.rows)
                    {
                        foreach (SPListItem item2 in items2)
                        {
                            Utility.CheckValidation(web, item2, list9, out sourceFieldArray, out sourceValue, out destValue, out compareAction);
                            if (!Utility.CompareCheck(compareAction, sourceValue, destValue))
                            {
                                List <string> list10   = sourceFieldArray.ToList <string>();
                                ErrorMessage  message2 = new ErrorMessage {
                                    FieldNames = list10,
                                    Message    = item2["Message"].ToString(),
                                    RowNumber  = num7
                                };
                                list.Add(message2);
                            }
                        }
                        num7++;
                    }
                }
                if (list.Count > 0)
                {
                    return(new JavaScriptSerializer().Serialize(list));
                }
                int result = 0;
                s = Utility.UpdateFiles(web, contractId, fields, addFiles, deleteFiles);
                if (s != "ok")
                {
                    return(s);
                }
                SPListItem item4 = (int.Parse(value2.value) > 0) ? list4.GetItemById(int.Parse(value2.value)) : list4.AddItem((contractId > 0) ? (list4.RootFolder.Url + "/" + contractId.ToString()) : list4.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.File);
                SPListItem item3 = setItemFields(fields, item4);
                SPItemSave save  = new SPItemSave {
                    ListId = guid,
                    Item   = item3,
                    Folder = web.GetFolder((contractId > 0) ? (list4.RootFolder.Url + "/" + contractId.ToString()) : list4.RootFolder.ServerRelativeUrl)
                };
                s = SaveItem(web, save.ListId, save.Item, save.Folder, contractId, web.CurrentUser.ID, viewersIds, editorsIds, creatorId);
                int.TryParse(s, out result);
                if (result > 0)
                {
                    s = "ok";
                    using (List <SPFieldValue> .Enumerator enumerator3 = list6.GetEnumerator())
                    {
                        while (enumerator3.MoveNext())
                        {
                            SPFieldValue detailField = enumerator3.Current;
                            SPFieldValue lk          = new SPFieldValue {
                                InternalName = detailField.value,
                                Type         = "Lookup",
                                value        = result.ToString()
                            };
                            Guid siteID = web.Site.ID;
                            SPSecurity.RunWithElevatedPrivileges(delegate {
                                using (SPSite site = new SPSite(siteID))
                                {
                                    using (SPWeb web1 = site.OpenWeb())
                                    {
                                        SPList detailList = web1.Lists[new Guid(detailField.LookupList)];
                                        SPListItemCollection detailItems = Utility.FindValidation(web, detailList.ID);
                                        SPFolder folder = web1.GetFolder((contractId > 0) ? (detailList.RootFolder.Url + "/" + contractId.ToString()) : detailList.RootFolder.ServerRelativeUrl);
                                        foreach (List <SPFieldValue> list2 in detailField.rows)
                                        {
                                            list2.Add(lk);
                                            SPFieldValue idValue  = list2.FirstOrDefault <SPFieldValue>(a => a.InternalName == "ID");
                                            SPListItem detailItem = setItemFields(list2, (int.Parse(value2.value) > 0) ? detailList.GetItemById(int.Parse(idValue.value)) : detailList.AddItem(folder.Url, SPFileSystemObjectType.File));
                                            SPItemSave detailsave = new SPItemSave {
                                                ListId = detailField.LookupList,
                                                Item   = detailItem,
                                                Folder = folder
                                            };
                                            saveItems.Add(detailsave);
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                string str7  = "";
                int    num10 = 0;
                foreach (SPItemSave save2 in saveItems)
                {
                    str7 = SaveItem(web, save2.ListId, save2.Item, save2.Folder, contractId, web.CurrentUser.ID, viewersIds, editorsIds, 0);
                }
                int.TryParse(str7, out num10);
                if ((num10 > 0) || ((s == "ok") && (saveItems.Count == 0)))
                {
                    s = Utility.CreateHistory(web, result, DateTime.Now.ToString(), "ثبت", "", listname, web.CurrentUser.ID);
                    if (s == "")
                    {
                        s = "ok";
                    }
                }
            }
            return(s);
        }