I have a custom list that has a GUID column which refers to the UniqueId of a document in a document library. Every time I upload a document, it should add an entry in this list and populate the column with this document GUID column.
List 1: Document Library, its items UniqueId is stored in the DocumentId column of the second list.
List 2: A custom list with a column called DocumentId where it's value is the document's UniqueId (GUID).
I have used an item receiver to do this, the code is shown below (working). This side works fine, the issue is when I want to do the opposite.
public override void ItemDeleting(SPItemEventProperties properties)
{
using (SPWeb web = properties.OpenWeb())
{
SPListItem documentItem = properties.ListItem;
string listUrl = SPUtility.ConcatUrls(web.ServerRelativeUrl, "Lists/PortalLinks");
// Get the PortalLinks list
SPList portalLinksList = web.GetList(listUrl);
// Gets the matching items by CAML
SPQuery linkQuery = new SPQuery();
linkQuery.Query = string.Format("<Where><Eq><FieldRef Name='DocumentItemId' /><Value Type='Guid'>{0}</Value></Eq></Where>", documentItem.UniqueId);
SPListItemCollection linkItemsMatchingGuid = portalLinksList.GetItems(linkQuery);
//add the main text to the stringbuilder
batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
foreach (SPListItem item in linkItemsMatchingGuid)
{
batchString.Append("<Method>");
batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
batchString.Append("</Method>");
}
batchString.Append("</Batch>");
web.ProcessBatchData(batchString.ToString());
}
base.ItemDeleting(properties);
}
If I delete the link item associated with a document, I also want to delete this document. So I went and added an ItemDeleting event receiver to this linked list but for some reason I can't seem to get this side to work.
Here is the ItemDeleting code to 'try' and delete the referencing document. (Not working)
public override void ItemDeleting(SPItemEventProperties properties)
{
using (SPWeb web = properties.OpenWeb())
{
// Get the matching list item beign deleted.
SPListItem linkItem = properties.ListItem;
Guid parentId = new Guid(linkItem["DocumentItemId"].ToString());
// Get the list of documents.
string documentLibraryUrl = SPUtility.ConcatUrls(web.ServerRelativeUrl, "/Documents");
SPList documentLibrary = web.GetList(documentLibraryUrl);
SPListItemCollection documents = documentLibrary.Items;
StringBuilder batchString = new StringBuilder();
batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
foreach (SPListItem item in documents)
{
if (item.UniqueId == parentId)
{
batchString.Append("<Method>");
batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
batchString.Append("</Method>");
}
}
batchString.Append("</Batch>");
web.ProcessBatchData(batchString.ToString());
}
base.ItemDeleting(properties);
}
I have tried the following before with no luck either:
Looping through the list and deleting the item one by one (I read its not very efficient so I switched to batch delete).
Result
Deleting the document removes the link in the list.
Deleting the link, however does NOT remove the referring document but I want it to.
Please let me know if there is a better way to do this.
Thanks.