Ошибка: «Объект используется в контексте, отличном от того, который связан с объектом» при добавлении поля поиска в список - PullRequest
0 голосов
/ 04 ноября 2019

В следующем коде я хочу добавить поле поиска в список. Однако он получил ошибку: «Объект используется в контексте, отличном от того, который связан с объектом». Однако я не знаю, где я ошибаюсь. Кто-нибудь может показать мне?

ClientContext context = new ClientContext("samplesite");
SecureString password = new SecureString();
foreach (char c in "abcdxyz".ToCharArray()) password.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("abcd@xyz.com",password);            
FieldLookup emp = context.CastTo<FieldLookup>(context.Site.RootWeb.Lists.GetByTitle("Employees").Fields.GetByTitle("First Name"));
context.Load(emp);
context.ExecuteQuery();
projs.Fields.AddDependentLookup("Leader", emp, "Employee");
emp.AllowMultipleValues = true;
projs.Fields.AddDependentLookup("Members", emp, "Employee");
context.ExecuteQuery();         

Ps: Консоль показывает адрес ошибки в строке: "projs.Fields.AddDependentLookup (" Leader ", emp," Employee ");"

1 Ответ

0 голосов
/ 04 ноября 2019

Кажется, "projs" это список объектов, поэтому я проверил это так:

using (ClientContext ctx = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
{

                ctx.Credentials = new SharePointOnlineCredentials(account, secret);
                ctx.Load(ctx.Web);
                ctx.ExecuteQuery();
                FieldLookup emp = ctx.CastTo<FieldLookup>(ctx.Site.RootWeb.Lists.GetByTitle("MyList").Fields.GetByTitle("MyLookup"));
                ctx.Load(emp);
                ctx.ExecuteQuery();
                ctx.Site.RootWeb.Lists.GetByTitle("MyList").Fields.AddDependentLookup("MyID", emp, "ID");
                emp.AllowMultipleValues = true;
                ctx.Site.RootWeb.Lists.GetByTitle("MyList").Fields.AddDependentLookup("MyCreatedDate", emp, "Created");
                ctx.ExecuteQuery();
}

enter image description here enter image description here

...