private static List <SPFieldGeneral> getFieldsFromList(SPList list, string contentTypeId)
        {
            SPContentType         type       = list.ContentTypes[contentTypeId];
            List <SPFieldGeneral> listFields = new List <SPFieldGeneral>();
            SPFieldCollection     fields     = (contentTypeId != "") ? list.ContentTypes[new SPContentTypeId(contentTypeId)].Fields : list.ContentTypes[0].Fields;

            foreach (SPField field in fields)
            {
                if ((field.Hidden || (field.FromBaseType && (field.InternalName != "Title"))) || ((field.InternalName == "Status") || (field.InternalName == "CurrentUser")))
                {
                    continue;
                }
                SPFieldGeneral general = new SPFieldGeneral();
                string         str     = field.Type.ToString();
                general.Guid         = field.Id;
                general.InternalName = field.InternalName;
                XmlDocument document = new XmlDocument();
                document.LoadXml(field.SchemaXml);
                XmlElement documentElement = document.DocumentElement;
                if (documentElement.HasAttribute("DisplayName"))
                {
                    general.Title = documentElement.GetAttribute("DisplayName");
                }
                else
                {
                    general.Title = field.Title;
                }
                general.Title   = field.GetProperty("DisplayName");
                general.Disable = field.GetProperty("Disable");
                general.NotShow = field.GetProperty("NotShow");
                string[] notShowArr = general.NotShow != null?general.NotShow.Split(',') : new string[]
                {
                };
                general.DefaultValue = field.DefaultValue;
                general.IsRequire    = field.Required;
                general.Type         = field.TypeAsString;
                general.Description  = field.Description;
                switch (field.TypeAsString)
                {
                case "Text":
                    general.MaxLength = ((SPFieldText)field).MaxLength;
                    break;

                case "Number":
                    general.MaxValue         = ((SPFieldNumber)field).MaximumValue;
                    general.MinValue         = ((SPFieldNumber)field).MinimumValue;
                    general.ShowAsPercentage = ((SPFieldNumber)field).ShowAsPercentage;
                    break;

                case "Lookup":
                    general.LookupList         = ((SPFieldLookup)field).LookupList.Replace("{", "").Replace("}", "");
                    general.LookupTitleField   = "Title";
                    general.LookupValueField   = "ID";
                    general.AllowMultipleValue = ((SPFieldLookup)field).AllowMultipleValues;
                    break;

                case "LookupMulti":
                    general.LookupList         = ((SPFieldLookup)field).LookupList.Replace("{", "").Replace("}", "");
                    general.LookupTitleField   = "Title";
                    general.LookupValueField   = "ID";
                    general.AllowMultipleValue = ((SPFieldLookup)field).AllowMultipleValues;
                    break;

                case "RelatedCustomLookupQuery":
                    general.LookupList         = field.GetCustomProperty("ListNameLookup").ToString().Replace("{", "").Replace("}", "");
                    general.LookupTitleField   = field.GetCustomProperty("FieldTitleLookup").ToString();
                    general.LookupValueField   = field.GetCustomProperty("FieldValueLookup").ToString();
                    general.AllowMultipleValue = ((SPFieldLookup)field).AllowMultipleValues;
                    general.RelatedFields      = field.GetCustomProperty("RelatedFields").ToString().Split(new char[] { '|' });
                    general.Query = field.GetCustomProperty("QueryLookup").ToString();
                    if ((field.GetCustomProperty("IsFile") != null) ? bool.Parse(field.GetCustomProperty("IsFile").ToString()) : false)
                    {
                        general.Type       = "File";
                        general.TypeFile   = field.GetCustomProperty("TypeFile").ToString();
                        general.VolumeFile = field.GetCustomProperty("VolumeFile").ToString();
                    }
                    break;

                case "MasterDetail":
                    general.LookupList       = field.GetCustomProperty("ListNameLookup").ToString();
                    general.RelatedFields    = field.GetCustomProperty("RelatedFields").ToString().Split(new char[] { '|' });
                    general.MasterLookupName = field.GetCustomProperty("MasterFieldNameLookup").ToString();
                    break;

                case "CustomComputedField":
                    general.LookupList          = field.GetCustomProperty("ListNameQuery").ToString();
                    general.LookupTitleField    = field.GetCustomProperty("FieldNameQuery").ToString();
                    general.Query               = field.GetCustomProperty("TextQuery").ToString();
                    general.AggregationFunction = field.GetCustomProperty("AggregatorFunction").ToString();
                    break;

                case "Choice":
                {
                    SPFieldChoice choice = (SPFieldChoice)field;
                    general.options = new List <string>();
                    foreach (string str2 in choice.Choices)
                    {
                        general.options.Add(str2);
                    }
                    general.DefaultValue       = ((SPFieldChoice)field).DefaultValue;
                    general.AllowMultipleValue = ((SPFieldChoice)field).ListItemMenu;
                    break;
                }

                case "MultiChoice":
                {
                    SPFieldMultiChoice choice2 = (SPFieldMultiChoice)field;
                    general.options = new List <string>();
                    foreach (string str2 in choice2.Choices)
                    {
                        general.options.Add(str2);
                    }
                    general.AllowMultipleValue = ((SPFieldMultiChoice)field).ListItemMenu;
                    break;
                }
                }
                if (!notShowArr.Contains("New"))
                {
                    listFields.Add(general);
                }
            }
            SPField        fieldByInternalName = list.Fields.GetFieldByInternalName("ID");
            SPFieldGeneral item = new SPFieldGeneral
            {
                Guid         = fieldByInternalName.Id,
                InternalName = fieldByInternalName.InternalName,
                Title        = fieldByInternalName.Title,
                DefaultValue = fieldByInternalName.DefaultValue,
                IsRequire    = fieldByInternalName.Required,
                Type         = fieldByInternalName.TypeAsString,
                Description  = fieldByInternalName.Description
            };

            listFields.Add(item);
            return(listFields);
        }
Пример #2
0
        public static List <SPFieldGeneral> GetFieldsList(string listId)
        {
            SPWeb web;

            try
            {
                web = SPContext.Current.Web;
            }
            catch (Exception)
            {
                web = new SPSite("http://net-sp:100").OpenWeb();
            }
            SPList list = web.Lists[new Guid(listId)];
            List <SPFieldGeneral> list2 = new List <SPFieldGeneral>();
            string str = HttpContext.Current.Request.Url.ToString();

            foreach (SPField field in list.Fields)
            {
                if ((field.FromBaseType && (field.InternalName != "Title")) && (field.InternalName != "ID"))
                {
                    continue;
                }
                SPFieldGeneral item = new SPFieldGeneral();
                string         str2 = field.Type.ToString();
                item.Guid         = field.Id;
                item.InternalName = field.InternalName;
                item.Title        = field.Title;
                item.DefaultValue = field.DefaultValue;
                item.IsRequire    = field.Required;
                item.Type         = field.TypeAsString;
                switch (field.TypeAsString)
                {
                case "Text":
                    item.MaxLength = ((SPFieldText)field).MaxLength;
                    break;

                case "Number":
                    item.MaxValue         = ((SPFieldNumber)field).MaximumValue;
                    item.MinValue         = ((SPFieldNumber)field).MinimumValue;
                    item.ShowAsPercentage = ((SPFieldNumber)field).ShowAsPercentage;
                    break;

                case "Lookup":
                    item.LookupList         = ((SPFieldLookup)field).LookupList.Replace("{", "").Replace("}", "");
                    item.LookupTitleField   = "Title";
                    item.LookupValueField   = "ID";
                    item.AllowMultipleValue = ((SPFieldLookup)field).AllowMultipleValues;
                    break;

                case "LookupMulti":
                    item.LookupList         = ((SPFieldLookup)field).LookupList.Replace("{", "").Replace("}", "");
                    item.LookupTitleField   = "Title";
                    item.LookupValueField   = "ID";
                    item.AllowMultipleValue = ((SPFieldLookup)field).AllowMultipleValues;
                    break;

                case "RelatedCustomLookupQuery":
                    item.LookupList       = field.GetCustomProperty("ListNameLookup").ToString().Replace("{", "").Replace("}", "");
                    item.LookupTitleField = field.GetCustomProperty("FieldTitleLookup").ToString();
                    item.LookupValueField = field.GetCustomProperty("FieldValueLookup").ToString();
                    item.RelatedFields    = field.GetCustomProperty("RelatedFields").ToString().Split(new char[] { '|' });
                    item.Query            = field.GetCustomProperty("QueryLookup").ToString();
                    break;

                case "MasterDetail":
                    item.LookupList       = field.GetCustomProperty("ListNameLookup").ToString();
                    item.RelatedFields    = field.GetCustomProperty("RelatedFields").ToString().Split(new char[] { '|' });
                    item.MasterLookupName = field.GetCustomProperty("MasterFieldNameLookup").ToString();
                    break;

                case "CustomComputedField":
                    item.LookupList          = field.GetCustomProperty("ListNameQuery").ToString();
                    item.LookupTitleField    = field.GetCustomProperty("FieldNameQuery").ToString();
                    item.Query               = field.GetCustomProperty("TextQuery").ToString();
                    item.AggregationFunction = field.GetCustomProperty("AggregatorFunction").ToString();
                    break;

                case "Choice":
                {
                    SPFieldChoice choice = (SPFieldChoice)field;
                    item.options = new List <string>();
                    foreach (string str3 in choice.Choices)
                    {
                        item.options.Add(str3);
                    }
                    item.DefaultValue       = ((SPFieldChoice)field).DefaultValue;
                    item.AllowMultipleValue = ((SPFieldChoice)field).ListItemMenu;
                    break;
                }

                case "MultiChoice":
                {
                    SPFieldMultiChoice choice2 = (SPFieldMultiChoice)field;
                    item.options = new List <string>();
                    foreach (string str3 in choice2.Choices)
                    {
                        item.options.Add(str3);
                    }
                    item.AllowMultipleValue = ((SPFieldMultiChoice)field).ListItemMenu;
                    break;
                }
                }
                list2.Add(item);
            }
            return(list2);
        }