spws
Version:
SharePoint Web Services Wrapper
249 lines • 11.8 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
// SPWS Library
var __1 = require("../..");
// Classes
var classes_1 = require("../../classes");
// Enum
var enum_1 = require("../../enum");
/**
* Creates an xml fields string
*/
var createFieldsXml = function (fields, type) {
// Keys that are excluded based on type of operation (command)
var excludedKeys = [
"Choices",
"Default",
"DefaultFormula",
"Formula",
"Validation",
"Name",
];
switch (type) {
case "New":
excludedKeys.push("EnforceUniqueValues", "Filterable");
break;
default:
break;
}
// Create xml
var xml = fields
// Iterate through each field
.map(function (field, index) {
// Create field xml string
var fieldXml = "";
switch (type) {
case "Delete":
fieldXml = "<Field Name=\"".concat(field.StaticName, "\"/>");
break;
case "New":
case "Update":
// Create field xml string
fieldXml = "<Field Name=\"".concat(field.StaticName, "\" ").concat(Object.entries(field).reduce(
// Iterate through each field key and prop
function (string, _a) {
var _b = __read(_a, 2), key = _b[0], prop = _b[1];
// Get value from prop
var value = "";
var fieldKey = key;
// Exclude keys which are not allowed
if (excludedKeys.includes(fieldKey))
return string;
// Switch by type of prop
switch (typeof prop) {
case "boolean":
value = prop ? "TRUE" : "FALSE";
break;
default:
value = prop;
break;
}
// Append to string
string += "".concat(fieldKey, "=\"").concat(value, "\" ");
// Return string (accumulator)
return string;
}, ""), ">");
break;
}
// Handle Field Types
switch (field.Type) {
case "Calculated":
if (typeof field.Formula === "string")
fieldXml += field.Formula;
break;
case "Choice":
case "MultiChoice":
// Handle Choice Fields
if (Array.isArray(field.Choices))
fieldXml += "<CHOICES>".concat(field.Choices.map(function (choice) { return "<CHOICE>".concat(choice, "</CHOICE>"); }).join(""), "</CHOICES>");
break;
default:
break;
}
// Handle Child Elements
if (typeof field.Validation === "string")
fieldXml += field.Validation;
if (typeof field.DefaultFormula === "string")
fieldXml += "<DefaultFormula>".concat(field.DefaultFormula, "</DefaultFormula>");
if (typeof field.DefaultFormulaValue === "string")
fieldXml += "<DefaultFormulaValue>".concat(field.DefaultFormulaValue, "</DefaultFormulaValue>");
if (typeof field.Default === "string")
fieldXml += "<Default>".concat(field.Default, "</Default>");
// Append to fieldXml
fieldXml += "</Field>";
// Return method
return "<Method ID=\"".concat(index + 1, "\">").concat(fieldXml, "</Method>\n ");
})
.join("");
// Return xml
return "<Fields>".concat(xml, "</Fields>");
};
/**
* Updates a list based on the specified field definitions and list properties.
*
* @link https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-services/ms774660(v=office.12)
* @link https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms437580(v=office.14)
* @example
* ```
* // Update list
* const res = await updateList({
* listName: "Announcements",
* webURL: "/sites/other",
* listProperties: { Description: "Demo description" },
* deleteFields: ["Age"],
* newFields: [
* {
* StaticName: "DateOfBirth",
* DisplayName: "Date of Birth",
* Type: "DateTime",
* Format: "DateOnly",
* },
* ],
*});
* ```
*/
var updateList = function (_a) {
var listName = _a.listName, _b = _a.webURL, webURL = _b === void 0 ? __1.defaults.webURL : _b, _c = _a.listProperties, listProperties = _c === void 0 ? {} : _c, _d = _a.deleteFields, deleteFields = _d === void 0 ? [] : _d, _e = _a.newFields, newFields = _e === void 0 ? [] : _e, _f = _a.updateFields, updateFields = _f === void 0 ? [] : _f;
return __awaiter(void 0, void 0, void 0, function () {
var req, res, error_1, error_2;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
req = new classes_1.SpwsRequest({
webService: enum_1.WebServices.Lists,
webURL: webURL,
soapAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateList",
});
// Create envelope
req.createEnvelope("<UpdateList xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">\n <listName>".concat(listName, "</listName>\n <listProperties>\n <List \n ").concat(Object.entries(listProperties).reduce(function (string, _a) {
var _b = __read(_a, 2), key = _b[0], prop = _b[1];
string += "".concat(key, "=\"").concat(prop, "\" ");
return string;
}, ""), "\n />\n </listProperties>\n <newFields>").concat(createFieldsXml(newFields.map(function (object) {
// Clone field
var field = __assign({}, object);
field.DisplayName = field.StaticName || field.DisplayName;
return field;
}), "New"), "</newFields>\n <updateFields>").concat(createFieldsXml(updateFields, "Update"), "</updateFields>\n <deleteFields><Fields>").concat(deleteFields
.map(function (field, index) { return "<Method ID=\"".concat(index + 1, "\"><Field Name=\"").concat(field, "\"/></Method>"); })
.join(""), "</Fields></deleteFields>\n <listVersion></listVersion>\n </UpdateList>\n"));
_g.label = 1;
case 1:
_g.trys.push([1, 7, , 8]);
return [4 /*yield*/, req.send()];
case 2:
res = _g.sent();
if (!(newFields.length > 0 &&
newFields.some(function (_a) {
var StaticName = _a.StaticName, DisplayName = _a.DisplayName;
return StaticName !== DisplayName;
}))) return [3 /*break*/, 6];
_g.label = 3;
case 3:
_g.trys.push([3, 5, , 6]);
// Resend the update to set correct display names
return [4 /*yield*/, updateList({ listName: listName, updateFields: newFields, webURL: webURL })];
case 4:
// Resend the update to set correct display names
_g.sent();
return [3 /*break*/, 6];
case 5:
error_1 = _g.sent();
console.error(error_1);
return [3 /*break*/, 6];
case 6:
// Return result
return [2 /*return*/, __assign(__assign({}, res), { data: undefined })];
case 7:
error_2 = _g.sent();
throw new classes_1.SpwsError(error_2);
case 8: return [2 /*return*/];
}
});
});
};
exports.default = updateList;
//# sourceMappingURL=updateList.js.map