public static void AttachFile(this SPListItem item, string filePath, string leafName, bool overwrite = false, bool updateRequired = false) { using (Stream fs = File.OpenRead(filePath)) { item.AttachFile(fs, leafName, overwrite, updateRequired); } }
public static void AttachFile(this SPListItem item, string filePath, bool overwrite = false, bool updateRequired = false) { string leafName = Path.GetFileName(filePath); item.AttachFile(filePath, leafName, overwrite, updateRequired); }
public static void AddDataTableToList(DataTable dt, SPList list, bool systemUpdate = false, bool doNotFireEvents = false) { if (dt == null) { return; } foreach (DataRow dr in dt.Rows) { SPListItem item = list.AddItem(); string[] attachments = null; foreach (DataColumn col in dt.Columns) { if (col.ReadOnly) { continue; } if (col.DataType == typeof(TaxonomyFieldValue)) { TaxonomyField taxonomyField = (TaxonomyField)list.Fields.GetField(col.ColumnName); taxonomyField.SetFieldValue(item, (TaxonomyFieldValue)(Convert.IsDBNull(dr[col]) ? new TaxonomyFieldValue(taxonomyField) : dr[col])); } else if (col.DataType == typeof(TaxonomyFieldValueCollection)) { TaxonomyField taxonomyField = (TaxonomyField)list.Fields.GetField(col.ColumnName); taxonomyField.SetFieldValue(item, (TaxonomyFieldValueCollection)(Convert.IsDBNull(dr[col]) ? new TaxonomyFieldValueCollection(taxonomyField) : dr[col])); } else if (col.ColumnName == "Attachments") { if (!Convert.IsDBNull(dr[col])) { attachments = dr[col].ToString().Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); } } else { item[col.ColumnName] = Convert.IsDBNull(dr[col]) ? null : dr[col]; } } if (attachments != null) { foreach (string attachment in attachments) { if (!string.IsNullOrEmpty(attachment)) { item.AttachFile(attachment.Trim(), false, true); } } } if (systemUpdate) { if (doNotFireEvents) { RunWithDisabledEvent(item.SystemUpdate); } item.SystemUpdate(); } else { if (doNotFireEvents) { RunWithDisabledEvent(item.Update); } item.Update(); } } }