3

I'm trying to modify two properties I have in several documents in my SharePoint library.

This is the code I use to get into the library:

public SPArchiveBDI(string sharepointsite, string documentLibraryName)
{
    _sharePointSite = sharepointsite;
    _documentLibraryName = documentLibraryName;

    context = new ClientContext(SharePointSite);
    list = context.Web.Lists.GetByTitle(DocumentLibraryName);
    context.Load(list);
    context.Load(list.RootFolder);
    context.ExecuteQuery();
    root = list.RootFolder;
    context.Load(root);
    context.ExecuteQuery();

    customerFolder = root.Folders.GetByUrl("לקוחות");
    context.Load(customerFolder);
    context.ExecuteQuery();

    bDIFolder = customerFolder.Folders.GetByUrl("BDI");
    context.Load(bDIFolder);
    context.ExecuteQuery();
}

This code works fine and I have no trouble using it.

This is the code I use to modify the properties of the document:

public void ChangeFileInBDI(string name, int WFID, int entityID)
{
    File doc = bDIFolder.Files.GetByUrl(name);
    context.Load(doc);
    context.ExecuteQuery();

    ListItem lst = doc.ListItemAllFields;

    lst["_x05de__x05e1__x05e4__x05e8__x0020__x05ea__x05d4__x05dc__x05d9__x05da_"] = WFID;
    lst["_x05d6__x05d9__x05d4__x05d5__x05d9__x0020__x05d9__x05d9__x05e9__x05d5__x05ea_"] = entityID;

    lst.Update();

    context.ExecuteQuery();
}

In the last line of the code the program falls and the error message I got says:

Additional information: Access denied. You do not have permission to perform these actions or to use that resource.

I looked it up and decided to use "RunWithElevatedPrivileges".

This is the code I tried to run:

public void ChangeFileInBDI(string name, int WFID, int entityID)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(context.Site.Id))
        {
            File doc = bDIFolder.Files.GetByUrl(name);
            context.Load(doc);
            context.ExecuteQuery();

            ListItem lst = doc.ListItemAllFields;

            lst["_x05de__x05e1__x05e4__x05e8__x0020__x05ea__x05d4__x05dc__x05d9__x05da_"] = WFID;
            lst["_x05d6__x05d9__x05d4__x05d5__x05d9__x0020__x05d9__x05d9__x05e9__x05d5__x05ea_"] = entityID;

            lst.Update();

            context.ExecuteQuery();
        }
    });
}

When I try to that code it falls in the line "using (SPSite site = new SPSite(context.Site.Id))".

The error message says that the property Id has not been initialized.

My context object is global and usually I have no problem using it.

What am I doing wrong?

Thank you in advance!

4
  • Did you get it working? Did my answer helps you?
    – Hardik
    Commented Jun 14, 2016 at 14:24
  • I think so, now I'm looking for a user with the permissions to write in the userName and password. Once it works I'll mark the answer. Commented Jun 14, 2016 at 14:47
  • You need service account credentials there.
    – Hardik
    Commented Jun 14, 2016 at 14:52
  • Did you tried it with your Username & Password?
    – Hardik
    Commented Jun 14, 2016 at 15:41

2 Answers 2

2

The purpose of Client Object model is to access SharePoint data from systems where the SharePoint is not installed like web services. Server object model is used to access data only SharePoint installed systems. If you want to access data from production environment and you are not having access to that machine, in this scenario you should use Client object model or web services. Client object model is similar to Server object model with few limitations, you can't access WebApplication and Farm objects, you can write our own web service or WCF or REST services to work on your own requirements which you can't accomplish with OOB Client object model.

client object model does not support SPSecurity.RunWithElevatedPrivileges.

You should replace SPSecurity.RunWithElevatedPrivileges code with below code.

public void ChangeFileInBDI(string name, int WFID, int entityID)
{
    // Use default authentication mode.
    context.AuthenticationMode = ClientAuthenticationMode.Default;  

    // Specify the credentials for the service account.
    context.Credentials = new SharePointOnlineCredentials("User Name", "Password");

            File doc = bDIFolder.Files.GetByUrl(name);
            context.Load(doc);
            context.ExecuteQuery();

            ListItem lst = doc.ListItemAllFields;

            lst["_x05de__x05e1__x05e4__x05e8__x0020__x05ea__x05d4__x05dc__x05d9__x05da_"] = WFID;
            lst["_x05d6__x05d9__x05d4__x05d5__x05d9__x0020__x05d9__x05d9__x05e9__x05d5__x05ea_"] = entityID;

            lst.Update();

            context.ExecuteQuery();

}

Don't forget to change User Name & Password to below line

 // Specify the credentials for the service account.
 context.Credentials = new SharePointOnlineCredentials("User Name", "Password");
1

In CSOM, RunWithElevatedPrivileges cannot be used. Code always runs under the context of current user.

1
  • So how should I change my code? Commented Jun 14, 2016 at 12:21

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.