-1

I am new to office 365 Developement. I want to write below code in CSOM in office 365 for a remote event receicer. But dont know exact methods.

SPUser loggedInUser;
SPSite spsite = null;
SPWeb spweb = null;

string strLoggedInUser = properties.UserLoginName;
loggedInUser = properties.Web.EnsureUser(strLoggedInUser);
try
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {

        using (spsite = properties.Site)
        {
            spsite.AllowUnsafeUpdates = true;
            using (spweb = properties.Web)
            {
                spweb.AllowUnsafeUpdates = true;
                SPList taskList = spweb.Lists["Tasks"];
                string filename = addedFile.Name.Substring(0, addedFile.Name.LastIndexOf('.')) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss") + addedFile.Name.Substring(addedFile.Name'.'));
                SPFile file = spweb.Files.Add(spweb.Url.ToString() + "/" + Tasks.Title.ToString() + "/" + filename, addedFile.OpenBinaryStream());

                file.Item["Author"] = loggedInUser.ID + ";#" + loggedInUser.LoginName;
                file.Item["Editor"] = loggedInUser.ID + ";#" + loggedInUser.LoginName;
                file.Update();
                Tasks.Update();
                spweb.AllowUnsafeUpdates = false;

            }
        }
    }
}
  • please post the complete code. You have posted incomplete code – Aakash Maurya Sep 1 '16 at 11:21
  • 1
    I need only for that code only. i just want to know what to write instead of using (spweb = properties.Web) n spsite = properties.Site in csom – Virkam Dev Sep 1 '16 at 11:30
4

As per your comment I am writing this answer.

If you are working with CSOM then there will be no class prefix as "SP". From your code I can say that this code you have written in Event receiver or Feature receiver. That's why it is getting Site and Web objects from properties.

For CSOM the code should like

ClientContext clientContext = new ClientContext(SiteUrl); clientContext.Credential = credentials; Site site = clientContext.Site; Web web = clientContext.Web;

Updates


As per your comment it seems that you are working with RER for SharePoint Online. From here you can get basic working code for your requirement.

In this scenario above code will not work and it should be used as shown in the given link.

using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
  {
    if (clientContext != null)
    {
      clientContext.Load(clientContext.Web);
      //do something
      clientContext.ExecuteQuery();
    }
  }
  • will this code work with event receiver?? – Virkam Dev Sep 1 '16 at 11:58
  • are trying to create event receiver for SharePoint Online with CSOM code? – Aakash Maurya Sep 1 '16 at 12:17
  • Yes a Remote Event Receiver for O365 – Virkam Dev Sep 1 '16 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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