/// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); using (SPSite site = properties.OpenSite()) { using (SPWeb web = site.OpenWeb()) { var destinationWeb = "http://becky.aepdev.com/sites/ET/Test1/cal1/cal3/"; using (SPSite destSite = new SPSite(destinationWeb)) { using (SPWeb destWeb = destSite.OpenWeb()) { var list = destWeb.Lists["cal4"]; var itemPromote = list.Items.Add(); itemPromote["Title"] = properties.ListItem["Title"]; itemPromote["RecurrenceData"] = properties.ListItem["RecurrenceData"]; itemPromote["EventType"] = properties.ListItem["EventType"]; itemPromote["EventDate"] = properties.ListItem["EventDate"]; itemPromote["EndDate"] = properties.ListItem["EndDate"]; itemPromote["UID"] = System.Guid.NewGuid(); itemPromote["TimeZone"] = 13; itemPromote["Recurrence"] = -1; itemPromote.Update(); } } } } }
/// <summary> /// An item was updated. /// </summary> public override void ItemUpdated(SPItemEventProperties properties) { base.ItemUpdated(properties); try { SPSecurity.RunWithElevatedPrivileges(delegate() { var spItem = properties.ListItem; if (bool.Parse(spItem["Converter"].ToString())) { var spFile = spItem.File; var pathSave = @"C:\Temp"; Download(spFile, pathSave); ConvertToPDF(pathSave); var filePdf = GetAllFile(pathSave).Single(f => Path.GetExtension(f).ToLower().Equals(".pdf") && f.Replace(Path.GetExtension(f), "").ToLower() == spFile.Name.Replace(Path.GetExtension(spFile.Name), "").ToLower()); Upload(filePdf, properties.List.Title, properties.OpenSite()); DeleteAllFiles(pathSave); } }); } catch (SPException ex) { throw ex; } }
/// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { // Get a reference to the site collection SPSite site = properties.OpenSite(); // Get a reference to the current site SPWeb currentWeb = properties.OpenWeb(); // Get a reference to the root site SPWeb rootWeb = site.RootWeb; // Skip if the root web is the same as the current web if (rootWeb.Url == currentWeb.Url) { return; } // Get the current list SPList currentList = properties.List; // Get the announcement list on the root site SPList rootList = rootWeb.Lists["Announcements"]; // Get the list item that got added SPListItem currentListItem = properties.ListItem; // Add the announcement item to the list on the root web SPListItem rootListItem = rootList.Items.Add(); foreach (SPField field in currentList.Fields) { if (!field.ReadOnlyField) { rootListItem[field.Id] = currentListItem[field.Id]; } } // Append user display name to title. rootListItem["Title"] += " - " + properties.UserDisplayName; // Append the web url to the Body rootListItem["Body"] += string.Format(" This announcements was made by {0} on subweb {1}", properties.UserLoginName, properties.WebUrl); rootListItem.Update(); }
public override void ItemUpdating(SPItemEventProperties properties) { if (properties.AfterProperties["ChildItem"] != null || properties.AfterProperties["ParentItem"] != null) { return; } bool bWorkspaceDriven = true; string pcGuid = ""; bool isCopyingToParent = false; var listItem = SaveDataJobExecuteCache.GetListItem(properties); try { pcGuid = listItem["ChildItem"].ToString(); } catch { } try{ pcGuid = listItem["ParentItem"].ToString(); isCopyingToParent = true; try { bWorkspaceDriven = bool.Parse(properties.AfterProperties["WorkspaceDriven"].ToString()); } catch { try { bWorkspaceDriven = bool.Parse(listItem["WorkspaceDriven"].ToString()); } catch { } } } catch { } if (pcGuid != "" && bWorkspaceDriven) { string[] itemInfo = pcGuid.Split('.'); try { Guid WebId = properties.Web.ID; SPWeb pWeb = properties.Web; Guid ListId = properties.ListId; int ListItemId = properties.ListItemId; SPList parentList = properties.List; ArrayList IgnoreFields = new ArrayList(); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = properties.OpenSite()) { using (SPWeb web = site.OpenWeb(new Guid(itemInfo[0]))) { SPList list = web.Lists[new Guid(itemInfo[1])]; SPListItem li = list.GetItemById(int.Parse(itemInfo[2])); if (isCopyingToParent) { li["ChildItem"] = WebId + "." + ListId + "." + ListItemId; IgnoreFields = new ArrayList(CoreFunctions.getConfigSetting(web, "EPMLiveCPSyncIgnore").ToLower().Split(',')); } else { li["ParentItem"] = WebId + "." + ListId + "." + ListItemId; IgnoreFields = new ArrayList(CoreFunctions.getConfigSetting(pWeb, "EPMLiveCPSyncIgnore").ToLower().Split(',')); } foreach (SPField f in parentList.Fields) { if (!f.ReadOnlyField && f.Reorderable && !IgnoreFields.Contains(f.InternalName.ToLower())) { try { SPField parentField = list.Fields.GetFieldByInternalName(f.InternalName); if (properties.AfterProperties[f.InternalName] != null) { li[parentField.Id] = properties.AfterProperties[f.InternalName]; } else { if (properties.List.Fields.ContainsFieldWithInternalName(f.InternalName)) { try { li[parentField.Id] = listItem[f.Id]; } catch { } } } } catch { } } } li.SystemUpdate(); } } }); } catch { } } }
/// <summary> /// An item was deleted. /// </summary> public override void ItemDeleted(SPItemEventProperties properties) { base.ItemDeleted(properties); string guidString; //If the Url ends with "default.dsep", presume it's an entity deletion. if (properties.BeforeUrl.EndsWith(Constants.DocumentStoreDefaultEntityPartFileName, StringComparison.InvariantCultureIgnoreCase)) { guidString = properties.BeforeUrl .Replace("/" + Constants.DocumentStoreDefaultEntityPartFileName, "") .Substring(properties.BeforeUrl.LastIndexOf("/", StringComparison.InvariantCulture) + 1); try { var entityId = new Guid(guidString); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityDeleted(context, entityId); return; } } } catch (Exception) { /* Do Nothing */ } } //If the above failed, and the Url ends with ".dsep", presume it's an entity part deletion. if (properties.BeforeUrl.EndsWith(Constants.DocumentSetEntityPartExtension, StringComparison.InvariantCultureIgnoreCase)) { var partName = properties.BeforeUrl .Substring(properties.BeforeUrl.LastIndexOf("/", System.StringComparison.InvariantCulture) + 1) .Replace(Constants.DocumentSetEntityPartExtension, ""); var urlWithoutPartName = properties.BeforeUrl .Replace("/" + partName + Constants.DocumentSetEntityPartExtension, ""); guidString = urlWithoutPartName.Substring(urlWithoutPartName.LastIndexOf("/", StringComparison.InvariantCulture) + 1); try { var entityId = new Guid(guidString); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityPartDeleted(context, entityId, partName); return; } } } catch (Exception) { /* Do Nothing */ } } //If the last fragment is a Guid, presume it's an entity deletion. guidString = properties.BeforeUrl .Substring(properties.BeforeUrl.LastIndexOf("/", StringComparison.InvariantCulture) + 1); try { var entityId = new Guid(guidString); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityDeleted(context, entityId); return; } } } catch (Exception) { /* Do Nothing */ } //If it wasn't a guid, presume an attachment. var fileName = properties.BeforeUrl .Substring(properties.BeforeUrl.LastIndexOf("/", System.StringComparison.InvariantCulture) + 1); var urlWithoutFileName = properties.BeforeUrl .Replace("/" + fileName, ""); guidString = urlWithoutFileName.Substring(urlWithoutFileName.LastIndexOf("/", StringComparison.InvariantCulture) + 1); try { var attachmentEntityId = new Guid(guidString); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); AttachmentDeleted(context, attachmentEntityId, fileName); // ReSharper disable RedundantJumpStatement return; // ReSharper restore RedundantJumpStatement } } } catch (Exception) { /* Do Nothing */ } //At this point, I don't know what it is (Folder or an additional file not in an document set), continue... }
/// <summary> /// Processes events. /// </summary> /// <param name="properties"></param> protected virtual void ProcessEvent(SPItemEventProperties properties) { //TODO: Should there be folder events too? What happens to the files in a folder when a folder is deleted? var documentStoreEntityContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityContentTypeId); var documentStoreEntityPartContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityPartContentTypeId); var attachmentContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityAttachmentContentTypeId); if (properties.ListItem.ContentTypeId.IsChildOf(documentStoreEntityContentTypeId)) { //Note: This is the actual Document Set. switch (properties.EventType) { case SPEventReceiverType.ItemFileMoved: { var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder); var entity = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, null); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var before = web.GetFolder(properties.BeforeUrl); var after = web.GetFolder(properties.AfterUrl); var oldFolder = SPDocumentStoreHelper.MapFolderFromSPFolder(before.ParentFolder); var newFolder = SPDocumentStoreHelper.MapFolderFromSPFolder(after.ParentFolder); var context = new SPBaristaContext(site, web); EntityMoved(context, entity, oldFolder, newFolder); } } break; } } } else if (properties.ListItem.ContentTypeId.IsChildOf(documentStoreEntityPartContentTypeId)) { if ( String.Compare(properties.ListItem.File.Name, Constants.DocumentStoreDefaultEntityPartFileName, StringComparison.InvariantCulture) == 0) { switch (properties.EventType) { case SPEventReceiverType.ItemAdded: { //TODO: See if this is appropraite. if (properties.ListItem.Folder == null) { return; } var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder); var entity = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File, null); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityAdded(context, entity); } } } break; case SPEventReceiverType.ItemUpdated: { if (properties.ListItem.Folder != null) { var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.Folder); var entity = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File, null); using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityUpdated(context, entity); } } } } break; } } else { var entityPart = SPDocumentStoreHelper.MapEntityPartFromSPFile(properties.ListItem.File, null); switch (properties.EventType) { case SPEventReceiverType.ItemAdded: using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityPartAdded(context, entityPart); } } break; case SPEventReceiverType.ItemUpdated: using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityPartUpdated(context, entityPart); } } break; } } } else if (properties.ListItem.ContentTypeId.IsChildOf(attachmentContentTypeId)) { var attachment = SPDocumentStoreHelper.MapAttachmentFromSPFile(properties.ListItem.File); switch (properties.EventType) { case SPEventReceiverType.ItemAdded: using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); AttachmentAdded(context, attachment); } } break; case SPEventReceiverType.ItemUpdated: using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); AttachmentUpdated(context, attachment); } } break; } } else if (properties.ListItem.File != null && properties.ListItem.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document) && String.Compare(properties.ListItem.File.Name, Constants.DocumentStoreDefaultEntityPartFileName, StringComparison.InvariantCultureIgnoreCase) == 0) { //Apparently the Default documents in a Doc Set are initially added as a "Document" var documentSet = DocumentSet.GetDocumentSet(properties.ListItem.File.ParentFolder); var entity = SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, properties.ListItem.File, null); switch (properties.EventType) { case SPEventReceiverType.ItemAdded: using (var site = properties.OpenSite()) { using (var web = properties.OpenWeb()) { var context = new SPBaristaContext(site, web); EntityAdded(context, entity); } } break; } } }
private static void CheckDefaultLang(SPItemEventProperties property) { try { SPWeb currentWeb = property.OpenWeb(); SPSite currentSite = property.OpenSite(); bool loadBalancingServersListExist = false; foreach (SPList list in currentWeb.Lists) { if (list.ToString() == "LoadBalancingServers") { if (list.ItemCount > 1) { loadBalancingServersListExist = true; } break; } } if (loadBalancingServersListExist) { SPList listLoadBalancingServers = currentWeb.Lists["LoadBalancingServers"]; SPListItem item = property.ListItem; SPList parentList = item.ParentList; if (parentList.ToString() == "TranslationContents" || (parentList.ToString() == "TranslationContentsSub")) { bool reloadCache = true; foreach (SPField currentField in item.Fields) { if (item[currentField.InternalName] != null) { if (item[currentField.InternalName].ToString().Contains("SPS_ADDED_")) { item[currentField.InternalName] = item[currentField.InternalName].ToString().Replace( "SPS_ADDED_", string.Empty); currentWeb.AllowUnsafeUpdates = true; item.SystemUpdate(false); currentWeb.AllowUnsafeUpdates = false; reloadCache = false; } } } if (reloadCache) { foreach (SPListItem server in listLoadBalancingServers.Items) { #pragma warning disable 612,618 ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); #pragma warning restore 612,618 string webId = string.Empty; if (parentList.ToString() == "TranslationContentsSub") { webId = "_" + item.Web.ID; } var req = (HttpWebRequest)WebRequest.Create(server["Title"] + "/_layouts/CacheControl.aspx?list=TranslationContents&webId=" + webId); req.Method = "GET"; req.Credentials = CredentialCache.DefaultCredentials; req.GetResponse(); } } } if (parentList.ToString() == "LanguagesVisibility" || parentList.ToString() == "Configuration Store") { foreach (SPListItem server in listLoadBalancingServers.Items) { #pragma warning disable 612,618 ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); #pragma warning restore 612,618 var req = (HttpWebRequest)WebRequest.Create(server["Title"] + "/_layouts/CacheControl.aspx?list=TranslationContentsOrLanguagesVisibility"); req.Method = "GET"; req.Credentials = CredentialCache.DefaultCredentials; req.GetResponse(); } } if (parentList.ToString() == "PagesTranslations") { foreach (SPListItem server in listLoadBalancingServers.Items) { #pragma warning disable 612,618 ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); #pragma warning restore 612,618 var req = (HttpWebRequest)WebRequest.Create(server["Title"] + "/_layouts/CacheControl.aspx?list=PagesTranslations"); req.Method = "GET"; req.Credentials = CredentialCache.DefaultCredentials; req.GetResponse(); } } } else { int serversCount = GetNumberOfFrontEndServer(); SPListItem item = property.ListItem; SPList parentList = item.ParentList; if ((parentList.ToString() == "TranslationContents") || (parentList.ToString() == "TranslationContentsSub") || (parentList.ToString() == "LanguagesVisibility")) { bool reloadCache = true; foreach (SPField currentField in item.Fields) { if (item[currentField.InternalName] != null) { if (item[currentField.InternalName].ToString().Contains("SPS_ADDED_")) { item[currentField.InternalName] = item[currentField.InternalName].ToString().Replace("SPS_ADDED_", string.Empty); currentWeb.AllowUnsafeUpdates = true; item.SystemUpdate(false); currentWeb.AllowUnsafeUpdates = false; reloadCache = false; } } } if (reloadCache) { if (serversCount > 1) { string webId = "_" + item.Web.ID; if (parentList.ToString().Equals("TranslationContents")) { webId = "_" + currentWeb.Site.WebApplication.Id; } CreateReloadCacheTimer(currentSite, currentWeb, "/_layouts/CacheControl.aspx?list=TranslationContents&webId=" + webId); } else { ResetSiteFirstLevelCache(currentWeb); if (parentList.ToString() == "TranslationContents" || parentList.ToString() == "TranslationContentsSub") { string webId = "_" + item.Web.ID; HttpRuntime.Cache.Remove("SPS_TRANSLATION_CACHE_IS_LOADED" + webId); HttpRuntime.Cache.Add("SPS_TRANSLATION_CACHE_IS_LOADED" + webId, "2", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } else { var cacheLoadedKey = new StringCollection(); IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator(); while (cacheEnum.MoveNext()) { string key = cacheEnum.Key.ToString(); if (key.IndexOf("SPS_TRANSLATION_CACHE_IS_LOADED") != -1) { cacheLoadedKey.Add(key); } } foreach (string key in cacheLoadedKey) { HttpRuntime.Cache.Remove(key); } } } } // Traitement pour update des resx if ((HttpRuntime.Cache["AlphamosaikResxFilesUpdate"] != null) && (bool)HttpRuntime.Cache["AlphamosaikResxFilesUpdate"]) { UpdateResxFiles(item); } } if (parentList.ToString() == "PagesTranslations") { if (serversCount > 1) { CreateReloadCacheTimer(currentSite, currentWeb, "/_layouts/CacheControl.aspx?list=PagesTranslations"); } else { HttpRuntime.Cache.Remove("AdminPagesToTranslate"); HttpRuntime.Cache.Remove("PagesNotToTranslate"); HttpRuntime.Cache.Remove("PagesToTranslate"); } } if (parentList.ToString() == "Configuration Store") { if (serversCount > 1) { CreateReloadCacheTimer(currentSite, currentWeb, "/_layouts/CacheControl.aspx?list=TranslationContentsOrLanguagesVisibility"); } else { var cacheLoadedKey = new StringCollection(); IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator(); while (cacheEnum.MoveNext()) { string key = cacheEnum.Key.ToString(); if (key.IndexOf("SPS_TRANSLATION_CACHE_IS_LOADED") != -1) { cacheLoadedKey.Add(key); } } foreach (string key in cacheLoadedKey) { HttpRuntime.Cache.Remove(key); } } } } currentWeb.Dispose(); currentWeb.Close(); } catch (Exception ex) { Utilities.LogException("Error in ReloadCacheEvent: " + ex.Message); } }