0

I've an event receiver in Sharepoint on-premise which updates a Hyperlink field with parent document set Url when a new item is added/updated.

Below is the code works good for me in SharePoint on-premise. How to perform the same step in SharePoint Online(CSOM) for Remote event receivers.

public void UpdateLink(SPItemEventProperties properties)
    {
        if (properties.ListItem.Folder == null && properties.ListItem.File != null)
        {
            string linkUrl = properties.Web.Url + "/" + properties.ListItem.ParentList.Title + "/Forms/" + getContentTypeName(properties.Web, properties.ListItem.File.ParentFolder.Item.ContentTypeId) + "/docsethomepage.aspx?List=" + properties.ListItem.ParentList.ID.ToString() + "&RootFolder=" + properties.Web.Url + "/" + properties.ListItem.ParentList.Title + "/" + properties.ListItem.File.ParentFolder.Name;
            properties.ListItem["LinkName"] = linkUrl + ", " + properties.ListItem.File.ParentFolder.Item.Title;
            this.EventFiringEnabled = false;
            properties.ListItem.SystemUpdate();
            this.EventFiringEnabled = true;
        }
    }

public string getContentTypeName(SPWeb web, SPContentTypeId contentTypeID)
    {
        SPContentTypeId ct = web.AvailableContentTypes.BestMatch(contentTypeID);
        SPContentType ctt = web.AvailableContentTypes[ct];
        return ctt.Name;
    }

There are many properties which are not available in Client side object model like "properties.ListItem.File.ParentFolder.Item.ContentTypeId"

How to achieve this using CSOM?

1 Answer 1

0

I've fixed it by the following CSOM code

string fileDirRef = ""; string caseUrl = ""; string parentDocSetName = "";
List photos = clientContext.Web.Lists.GetById(guid);
ListItem item = photos.GetItemById(p);
clientContext.Load(item);
clientContext.ExecuteQuery();
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();

if ((item.FileSystemObjectType == FileSystemObjectType.File))
            {
                fileDirRef = item["FileDirRef"].ToString();
                caseUrl = clientContext.Web.Url + "/_layouts/15/DocSetHome.aspx?id=" + fileDirRef;

                parentDocSetName = fileDirRef.Substring(fileDirRef.LastIndexOf('/') + 1);
                item["LinkName"] = caseUrl + ", " + parentDocSetName;

                item.SystemUpdate();

                clientContext.ExecuteQuery();
            }

Hope this will help someone!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.