protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { try { using (SPSite sourceSite = new SPSite(__Context.Web.Site.ID, __Context.Site.UserToken)) { using (SPWeb sourceWeb = sourceSite.OpenWeb(this.__Context.Web.ID)) { //replace any workflow variables string destinationUrlProcessed = Helper.ProcessStringField(DestinationListURL, executionContext.Activity, __Context); using (SPSite destSite = new SPSite(destinationUrlProcessed, __Context.Site.UserToken)) { using (SPWeb destWeb = destSite.OpenWeb()) { SPList destinationList = null; //each list, even a non document library list has at least a root folder. SPFolder destFolder = destWeb.GetFolder(destinationUrlProcessed); if (!destFolder.Exists) { throw new ApplicationException(string.Format("List at {0} does not exist!", DestinationListURL)); } destinationList = destWeb.Lists[destFolder.ParentListId]; SPList sourceList = sourceWeb.Lists[new Guid(SourceListID)]; SPListItem sourceItem = sourceList.Items.GetItemById(SourceListItemID); ItemCopier.ListItemCopyOptions options = new ItemCopier.ListItemCopyOptions(); options.IncludeAttachments = true; options.OperationType = ItemCopier.OperationType.Copy; options.Overwrite = true; options.DestinationFolder = destFolder; using (ItemCopier myCopier = new ItemCopier(sourceItem, destinationList, options)) { myCopier.UpdateItem(DestinationItemID); string message = "Item Updated item at" + DestinationListURL + "; ID: " + DestinationItemID + "; with data from list:" + SourceListID + "; item:" + SourceListItemID; WorkflowHistoryLogger.LogMessage(executionContext, SPWorkflowHistoryEventType.None, "Complete", UserID, message); } } } } } } catch (Exception ex) { WorkflowHistoryLogger.LogError(executionContext, UserID, ex); return(ActivityExecutionStatus.Faulting); } return(ActivityExecutionStatus.Closed); }
public override void ItemUpdated(SPItemEventProperties properties) { SPListItem PublishedOriginal = null; bool updatePublishedToField = false; try { PublishedOriginal = properties.ListItem; StringBuilder destinationURLs = new StringBuilder(ColumnHelper.GetFieldValue(PublishedOriginal, ColumnHelper.PublishedToInternalColumnName)); string[] destinationUrlArray = destinationURLs.ToString().Split(';'); foreach (string destinationDisplayUrl in destinationUrlArray) { SPListItem destinationItem = ListHelper.GetSPItemFromURL(destinationDisplayUrl); if (destinationItem != null) { if (ColumnHelper.ListContainsField(destinationItem.ParentList, ColumnHelper.PublishedFromInternalColumnName)) { ItemCopier.ListItemCopyOptions options = new ItemCopier.ListItemCopyOptions(); options.IncludeAttachments = true; options.OperationType = ItemCopier.OperationType.Copy; options.Overwrite = true; options.DestinationFolder = ListHelper.GetSPFolderFromURL(destinationDisplayUrl); options.LinkToOriginal = true; using (ItemCopier myCopier = new ItemCopier(PublishedOriginal, destinationItem.ParentList, options)) { myCopier.UpdateItem(destinationItem.ID); } } else //published from column has been deleted, treat this as an unlinked list and do not syndicate the update { string old = destinationURLs.ToString(); destinationURLs = destinationURLs.Replace(destinationDisplayUrl, ""); destinationURLs = new StringBuilder(destinationURLs.Replace("; ;", "; ").ToString().TrimStart(';').Trim()); updatePublishedToField = true; } } if (updatePublishedToField) { ColumnHelper.SetFieldValue(PublishedOriginal, ColumnHelper.PublishedToInternalColumnName, destinationURLs.ToString()); PublishedOriginal.SystemUpdate(); } } } catch { //if exception was caused because the published to field has been removed from this list, //remove the event handler if (!ColumnHelper.ListContainsField(properties.ListItem.ParentList, ColumnHelper.PublishedToInternalColumnName)) { RemoveEventReceiver(properties.ListItem.ParentList); } } base.ItemUpdated(properties); }
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { try { using (SPSite sourceSite = new SPSite(__Context.Web.Site.ID, __Context.Site.UserToken)) { using (SPWeb sourceWeb = sourceSite.OpenWeb(this.__Context.Web.ID)) { SPList sourceList = sourceWeb.Lists[new Guid(ListID)]; //replace any workflow variables string destinationUrls = Helper.ProcessStringField(DestinationListUrl, executionContext.Activity, __Context); ColumnHelper.EnsurePublishColumns(sourceList, destinationUrls); SPListItem sourceItem = sourceList.Items.GetItemById(ListItemID); StringBuilder resultLinks = new StringBuilder(); string[] destinationListUrls = destinationUrls.Trim().Trim(';').Split(';'); foreach (string destinationUrl in destinationListUrls) { SPFolder destFolder = ListHelper.GetSPFolderFromURL(destinationUrl); if (!destFolder.Exists) { throw new ApplicationException(string.Format("List at {0} does not exist!", destinationUrl)); } SPList destinationList = ListHelper.GetSPListFromURL(destinationUrl, __Context.Site.UserToken); ItemCopier.ListItemCopyOptions options = new ItemCopier.ListItemCopyOptions(); options.IncludeAttachments = true; options.OperationType = ItemCopier.OperationType.Copy; options.Overwrite = true; options.DestinationFolder = destFolder; options.LinkToOriginal = true; using (ItemCopier myCopier = new ItemCopier(sourceItem, destinationList, options)) { int itemID = GetExistingID(destinationList, destinationUrl); if (itemID > 0) { myCopier.UpdateItem(itemID); string message = "Published item updated from List: " + ListID + "; item: " + ListItemID + "; to url:" + DestinationListUrl + "; new id: " + itemID; WorkflowHistoryLogger.LogMessage(executionContext, SPWorkflowHistoryEventType.None, "Complete", UserID, message); } else { itemID = myCopier.Copy(); using (SPWeb destWeb = destinationList.ParentWeb) { string newURL = destWeb.Url + "/" + destinationList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?id=" + itemID + " ; "; resultLinks.Append(newURL.ToLower()); string message = "New Item Published from List: " + ListID + "; item: " + ListItemID + "; to url:" + newURL; WorkflowHistoryLogger.LogMessage(executionContext, SPWorkflowHistoryEventType.None, "Complete", UserID, message); } } } } string currentLinks = (string)sourceItem[ColumnHelper.PublishedToInternalColumnName]; sourceItem[ColumnHelper.PublishedToInternalColumnName] = resultLinks.ToString() + currentLinks; sourceItem.SystemUpdate(); } } } catch (Exception ex) { WorkflowHistoryLogger.LogError(executionContext, UserID, ex); return(ActivityExecutionStatus.Faulting); } return(ActivityExecutionStatus.Closed); }