1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
static void Main(string[] args) {
SPSite siteCollection = new SPSite("http://localhost/");
SPWeb site = siteCollection.OpenWeb("/testImport");
SPList changeRequestList = null;
EnsureListExists(site, "imported");
changeRequestList = site.Lists["imported"];
site.AllowUnsafeUpdates = true;
EnsureFieldExists(changeRequestList, "Name", SPFieldType.Text);
InsertData<string>(changeRequestList, "Name", "Test");
}
private static void EnsureFieldExists(SPList list, string fieldName, SPFieldType sPFieldType) {
foreach (SPField field in list.Fields) {
if (field.Title == fieldName) {
return;
}
}
list.Fields.Add(fieldName, sPFieldType, false);
list.Update();
}
private static void InsertData<T>(SPList list, string fieldName, T fieldValue) {
SPListItem item = list.Items.Add();
item[fieldName] = fieldValue;
item.Update();
}
private static void EnsureListExists(SPWeb site, string listName) {
foreach (SPList list in site.Lists) {
if (list.Title == listName) {
return;
}
}
site.Lists.Add(listName, "List of all data for the project", SPListTemplateType.GenericList);
site.Update();
}
} |
Partager