public void UploadFile(String targetLibraryName, String fileName, byte[] file, Boolean overwrite) { SPList targetLibrary = web.Lists[targetLibraryName]; String destUrl = SPUtility.ConcatUrls(web.Url, targetLibrary.RootFolder.Url) + "/" + fileName; SPFile newFile = web.GetFile(destUrl); if (newFile.Exists) { if (newFile.CheckOutType != SPFile.SPCheckOutType.None) { newFile.CheckIn(checkInComment); } using (EventReceiverManager eventReceiverManager = new EventReceiverManager(true)) { newFile.Delete(); } } SPFieldUserValue userValue = new SPFieldUserValue(web, listItemForm[SPBuiltInFieldId.Author].ToString()); SPUser author = userValue.User; SPUserToken userToken = author.UserToken; using (SPSite impSite = new SPSite(web.Site.ID, userToken)) { using (SPWeb impWeb = impSite.OpenWeb()) { SPList impTargetLibrary = impWeb.Lists[targetLibraryName]; String impDestUrl = SPUtility.ConcatUrls(impWeb.Url, impTargetLibrary.RootFolder.Url) + "/" + fileName; SPFile upload = impTargetLibrary.RootFolder.Files.Add(impDestUrl, file, overwrite); } } }
public void Initialize() { shimsContext = ShimsContext.Create(); SetupShims(); eventReceiverManager = new EventReceiverManager(new ShimSPWeb()); privateObject = new PrivateObject(eventReceiverManager); privateType = new PrivateType(typeof(EventReceiverManager)); }
/// <summary> /// This method is fake. All the parameters are required, even though not all of them are used /// </summary> private void AddEventReceiver( EventReceiverManager eventReceiver, SPList list, string assembly, string className, SPEventReceiverType type, ref XElement element) { element.Add(DummyString); AddEventReceiverWasCalled = true; }
public void Constructor_Should_CreatesInstance() { // Arrange var spWeb = new ShimSPWeb().Instance; // Act var instance = new EventReceiverManager(spWeb); privateObject = new PrivateObject(instance); var web = privateObject.GetFieldOrProperty("Web") as SPWeb; // Assert instance.ShouldSatisfyAllConditions( () => instance.ShouldNotBeNull(), () => web.ShouldNotBeNull(), () => web.ShouldBe(spWeb)); }
/// <summary> /// This method is fake. All the parameters are required, even though not all of them are used /// </summary> private void ListEventReceivers(EventReceiverManager eventReceiver, SPList list, ref XElement element) { element.Add(DummyString); ListEventReceiversWasCalled = true; }
/// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); //Ausführen des folgenden Codes nur, wenn ein XML vorhanden ist if (properties.ListItem.Name.Contains(".xml")) { //Durch folgende Zeile wird der enthaltene Code als SHAREPOINT\System ausgeführt und nicht unter dem Kontext des Benutzers, der den Auftrag unterschrieben hat SPSecurity.RunWithElevatedPrivileges(delegate() { //Laden des aktuellen Webs; das ist nötig, um alle möglichen Operationen mit Bibliotheken und Elementen auszuführen //dieses wird wieder geschlossen bzw. freigegeben, wenn die geschweifte Klammer von "using" geschlossen wird using (SPSite site = new SPSite(properties.WebUrl)) { using (SPWeb web = site.OpenWeb()) { string orderNumberCol = "FileLeafRef"; string orderPdfLibraryName = "Auftragszettel-Archiv"; string attachmentClm = "Anlagen/Begründung"; string attachmentVIClm = "Anlagen/Begründung (VI)"; string attachmentDesc = "öffnen"; SPListItem _currentItem = properties.ListItem; string title = _currentItem.Title.ToString(); string logFile = @"C:\Bestellung\Logs\EventReceivers\OrderPdfArchived\" + title + ".log"; StringBuilder sb = new StringBuilder(); sb.AppendLine("OrderForm " + title + " was added"); string orderNumber = Path.GetFileNameWithoutExtension(_currentItem[orderNumberCol].ToString()); SPList orderPdfLibrary = web.Lists.TryGetList(orderPdfLibraryName); sb.AppendLine("Got list '" + orderPdfLibrary.Title + "'"); string folder = _currentItem.File.ParentFolder.ToString(); sb.AppendLine("Try getting folder '" + folder + "'..."); var nFolder = web.GetFolder(String.Format("{0}/Auftragsformular-Archiv/" + folder, web.Url)); sb.AppendLine("Got folder '" + nFolder.Url + "'"); sb.AppendLine("Querying for archived pdf... "); SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + orderNumber + ".pdf</Value></Eq></Where>"; query.Folder = nFolder; SPListItemCollection queriedOrderPdfsItems = orderPdfLibrary.GetItems(query); int count = queriedOrderPdfsItems.Count; sb.AppendLine("Found archived pdfs... " + count.ToString()); if (count == 1) { sb.AppendLine("Changing URLs for one pdf... "); string attachmentUrl = SPUtility.ConcatUrls(web.ServerRelativeUrl, "/_layouts/FormServer.aspx?XMLLocation=/bestellung/" + folder + "/" + orderNumber + ".xml&OpenIn=Browser&DefaultView=Anlagen&Source=/bestellung/SitePages/Schliessen.aspx"); SPFieldUrlValue url = new SPFieldUrlValue(); url.Description = attachmentDesc; url.Url = attachmentUrl; string attachmentUrlVI = SPUtility.ConcatUrls(web.ServerRelativeUrl, "/_layouts/FormServer.aspx?XMLLocation=/bestellung/" + folder + "/" + orderNumber + ".xml&OpenIn=Browser&DefaultView=AnlagenWeitergabe&Source=/bestellung/SitePages/Schliessen.aspx"); SPFieldUrlValue urlVI = new SPFieldUrlValue(); urlVI.Description = attachmentDesc; urlVI.Url = attachmentUrlVI; sb.AppendLine("Url for collumn " + attachmentClm + " : " + url.Url.ToString()); sb.AppendLine("Url for collumn " + attachmentVIClm + " : " + urlVI.Url.ToString()); foreach (SPListItem orderPdf in queriedOrderPdfsItems) { sb.AppendLine("Iterating through found pdfs, should be only one..."); using (EventReceiverManager eventReceiverManager = new EventReceiverManager(true)) { sb.AppendLine("Try updating item " + orderPdf.Title.ToString() + "..."); orderPdf[attachmentClm] = url; orderPdf[attachmentVIClm] = urlVI; orderPdf.SystemUpdate(false); sb.AppendLine("Item updated"); } } } else { sb.AppendLine("There we're found more or less than one archived pdf... do nothing and quit"); } File.AppendAllText(logFile, sb.ToString()); } } }); } }
/// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); //Ausführen des folgenden Codes nur, wenn ein PDF vorhanden ist if (properties.ListItem.Name.Contains(".pdf")) { //Durch folgende Zeile wird der enthaltene Code als SHAREPOINT\System ausgeführt und nicht unter dem Kontext des Benutzers, der den Auftrag unterschrieben hat SPSecurity.RunWithElevatedPrivileges(delegate() { //Laden des aktuellen Webs; das ist nötig, um alle möglichen Operationen mit Bibliotheken und Elementen auszuführen //dieses wird wieder geschlossen bzw. freigegeben, wenn die geschweifte Klammer von "using" geschlossen wird using (SPSite site = new SPSite(properties.WebUrl)) { using (SPWeb web = site.OpenWeb()) { //Summe korrigieren string orderPdfSumClm = "Summe"; string orderPdfSumField = "Summe"; string orderPdfNumberClm = "Auftragsnummer"; SPListItem _currentItem = web.Lists[properties.ListId].GetItemById(properties.ListItemId); string title = _currentItem.Title.ToString(); string logFile = @"C:\Bestellung\Logs\EventReceivers\OrderPdfArchived\" + title + ".log"; StringBuilder sb = new StringBuilder(); sb.AppendLine("OrderPdf " + title + " was added"); sb.AppendLine("Check if fields exist..."); bool sumFieldExists = _currentItem.Fields.ContainsField(orderPdfSumClm); bool orderIdFieldExists = _currentItem.Fields.ContainsField(orderPdfNumberClm); sb.AppendLine(orderPdfSumClm + ": " + sumFieldExists.ToString()); sb.AppendLine(orderPdfNumberClm + ": " + orderIdFieldExists.ToString()); if (sumFieldExists && orderIdFieldExists) { sb.AppendLine("Fields exist, go on..."); string orderId = _currentItem[orderPdfNumberClm].ToString(); decimal sumClmValue = decimal.Parse(_currentItem[orderPdfSumClm].ToString(), NumberStyles.Currency); sb.AppendLine("Sum in collumn is: " + sumClmValue.ToString()); sb.AppendLine("Opening pdf..."); decimal sumPdfValue; SPFile pdfFile = _currentItem.File; byte[] pdfBytes = pdfFile.OpenBinary(); using (PdfReader pdfreader = new PdfReader(pdfBytes)) { sb.AppendLine("Pdf opened with iText..."); AcroFields fields = pdfreader.AcroFields; sumPdfValue = decimal.Parse(fields.GetField(orderPdfSumField), NumberStyles.Currency); sb.AppendLine("Sum in pdf is: " + sumPdfValue.ToString()); } sb.AppendLine("Checking if sum values differ..."); if (sumClmValue != sumPdfValue) { sb.AppendLine("Sums are different. Assiging new sum (" + sumPdfValue + ") to collumn..."); double sumPdfValueDouble = (double)sumPdfValue; _currentItem[orderPdfSumClm] = sumPdfValueDouble; } else { sb.AppendLine("Sums are not different, carry on..."); } //Gruppe eintragen sb.AppendLine("Set the group collumn..."); string groupStr = ""; string orderPdfGroupClm = "Gruppe"; string orderFormGroupClm = "Gruppe"; string orderPdfIDDBClm = "IDDB"; string orderFormLibrary = "Auftragsformular"; string orderFormArchiveLibrary = "Auftragsformular-Archiv"; string orderFormIdStr = _currentItem[orderPdfIDDBClm].ToString(); sb.AppendLine("Got the ID of InfoPath form: " + orderFormIdStr); string orderNumber = _currentItem[orderPdfNumberClm].ToString(); sb.AppendLine("Got the orderNumber"); decimal orderFormIdDec = Convert.ToDecimal(orderFormIdStr); int orderFormId = Convert.ToInt32(orderFormIdDec); SPList orderFormList = web.Lists.TryGetList(orderFormLibrary); sb.AppendLine("Got the library " + orderFormList.Title.ToString()); //Prüfen, ob das Auftragsformular noch nicht in das Archiv verschoben wurde //Es wird die Anzahl aller Elemente, die die obrige ID haben, gespeichert. sb.AppendLine("Trying to find Info-Path forms with ID " + orderFormId); int countFoundOrderFormItems = (from SPListItem item in orderFormList.Items where Convert.ToInt32(item["ID"]) == orderFormId select item).Count(); if (countFoundOrderFormItems > 0) { //Das Auftragsformular wurde noch nicht verschoben sb.AppendLine("Forms were found: " + countFoundOrderFormItems.ToString()); sb.AppendLine("Trying to get the one form..."); SPListItem orderFormItem = orderFormList.GetItemById(orderFormId); groupStr = orderFormItem[orderFormGroupClm].ToString(); sb.AppendLine("Got form and group " + groupStr); SPGroup group = web.Groups[groupStr]; string groupLogin = group.ID.ToString() + ";#" + group.LoginName.ToString(); sb.AppendLine("Assigning group " + groupLogin + " to collumn"); _currentItem[orderPdfGroupClm] = groupLogin; } else //Das Auftragsformular wurde ins Archiv verschoben { sb.AppendLine("No form was found..."); } using (EventReceiverManager eventReceiverManager = new EventReceiverManager(true)) { sb.AppendLine("Try updating item " + _currentItem.Title.ToString()); _currentItem.SystemUpdate(false); sb.AppendLine("Item updated"); } } else { sb.AppendLine("Required fields do not exist. Do nothing and quit."); } File.AppendAllText(logFile, sb.ToString()); } } }); } }