Microsoft.SharePoint.Client.ServerException - User cannot be found - c#

Get list files from sharepoint library:
using (ClientContext cxt = new ClientContext("Site/Library"))
{
List doclib = cxt.Web.Lists.GetByTitle(listTitle);
cxt.Load(doclib, l => l.RootFolder, l => l.Fields);
cxt.ExecuteQuery();
do
{
CamlQuery query = new CamlQuery();
query.ListItemCollectionPosition = pos;
query.ViewXml = String.Format(#"<View Scope='RecursiveAll'><RowLimit>{0}</RowLimit></View>", PartCount);
ListItemCollection listItems = doclib.GetItems(query);
cxt.Load(
listItems,
a => a.ListItemCollectionPosition,
a => a.Include(
b => b["FileRef"],
b => b.File,
b => b.File.Author).Where(b => (string)b["FSObjType"] != "1"));
cxt.ExecuteQuery();
pos = listItems.ListItemCollectionPosition;
}
while (pos != null);
}
without the expression "b => b.File.Author" works well, on another library everything works with this expression too. Exception:
Microsoft.SharePoint.Client.ServerException
Data:
Message:
User cannot be found.
Stack:
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

Related

C# Solr boost not being applied to generated query

I'm using Solr 7.5 with Sitecore 9.2. I have some complex code that generates a search query, which I'm now trying to apply boosting to to prioritize page items over media assets (the relevant lines begin at isAssetPredicate....
public override Expression<Func<T, bool>> Build()
{
string query = _values.FirstOrDefault();
string queryLower = query.ToLower();
if (string.IsNullOrEmpty(query)) return null;
var predicate = PredicateBuilder.False<T>();
var exactPredicate = PredicateBuilder.False<T>();
exactPredicate = exactPredicate.Or(x => x.ItemPartNumberLower.Contains(queryLower)).Boost(90);
exactPredicate = exactPredicate.Or(x => x.TitleLower.Contains(queryLower)).Boost(99);
exactPredicate = exactPredicate.Or(x => x.Keywords.Contains(queryLower)).Boost(9);
exactPredicate = exactPredicate.Or(x => x.FileType.Contains(queryLower)).Boost(9);
exactPredicate = exactPredicate.Or(x => x.AggregatedSearchFields.Contains(queryLower)).Boost(9);
var fuzzyPredicate = PredicateBuilder.False<T>();
fuzzyPredicate = fuzzyPredicate.Or(x => x.ItemPartNumberLower.Contains(queryLower)).Boost(90);
fuzzyPredicate = fuzzyPredicate.Or(x => x.TitleLower.Contains(queryLower)).Boost(99);
fuzzyPredicate = fuzzyPredicate.Or(x => x.Keywords.Contains(queryLower)).Boost(9);
fuzzyPredicate = fuzzyPredicate.Or(x => x.FileType.Contains(queryLower)).Boost(9);
fuzzyPredicate = fuzzyPredicate.Or(x => x.AggregatedSearchFields.Contains(queryLower)).Boost(9);
// try searching for each term separately rather than exact match
var queryParts = queryLower.Trim().Split(' ');
if (queryParts.Length > 1)
{
var partsPredicate = PredicateBuilder.True<T>();
var fuzzyPartsPredicate = PredicateBuilder.False<T>();
foreach (var part in queryParts)
{
if (String.IsNullOrEmpty(part)) continue;
var subQuery = PredicateBuilder.False<T>();
subQuery = subQuery.Or(x => x.TitleLower.Contains(part)).Boost(99);
subQuery = subQuery.Or(x => x.ItemPartNumberLower.Contains(part)).Boost(95);
subQuery = subQuery.Or(x => x.Keywords.Contains(part)).Boost(80);
subQuery = subQuery.Or(x => x.FileType.Contains(part)).Boost(1);
subQuery = subQuery.Or(x => x.AggregatedSearchFields.Contains(part)).Boost(1);
partsPredicate = partsPredicate.And(subQuery);
var subQueryFuzzy = PredicateBuilder.False<T>();
subQueryFuzzy = subQueryFuzzy.Or(x => x.TitleLower.Like(part)).Boost(99);
subQueryFuzzy = subQueryFuzzy.Or(x => x.ItemPartNumberLower.Like(part)).Boost(95);
subQueryFuzzy = subQueryFuzzy.Or(x => x.Keywords.Like(part)).Boost(80);
subQueryFuzzy = subQueryFuzzy.Or(x => x.FileType.Like(part)).Boost(1);
subQueryFuzzy = subQueryFuzzy.Or(x => x.AggregatedSearchFields.Like(part)).Boost(1);
fuzzyPartsPredicate = fuzzyPartsPredicate.Or(subQueryFuzzy);
}
exactPredicate = exactPredicate.Or(partsPredicate).Boost(1);
fuzzyPredicate = fuzzyPredicate.Or(fuzzyPartsPredicate).Boost(1);
}
var queryPredicate = PredicateBuilder.False<T>();
queryPredicate = queryPredicate.Or(exactPredicate).Boost(99);
queryPredicate = queryPredicate.Or(fuzzyPredicate).Boost(1);
predicate = predicate.Or(queryPredicate);
if (HasAttribute(FieldType.Title))
{
predicate = predicate.Or(GetQueryExpression(FieldType.Title, query));
}
if (HasAttribute(FieldType.Description))
{
predicate = predicate.Or(GetQueryExpression(FieldType.Description, query));
}
if (HasAttribute(FieldType.Body))
{
predicate = predicate.Or(GetQueryExpression(FieldType.Body, query));
}
else if (QueryFormatter.NeedsFormatting(query))
{
predicate = predicate.Or(x => x.Content.MatchWildcard(QueryFormatter.FormatQuery(query)));
}
else
{
predicate = predicate.Or(x => x.Content.Like(query, SiteSettings.MinimumSimilarity));
}
var isAssetPredicate = PredicateBuilder.True<T>().And(x => x.IsAsset.Boost(0.0000001f)).And(predicate).Boost(0.0000001f);
var isPagePredicate = PredicateBuilder.True<T>().And(x => !x.IsAsset.Boost(9999f)).And(predicate).Boost(9f);
var finalPredicate = PredicateBuilder.True<T>();
finalPredicate = finalPredicate.Or(isAssetPredicate.Boost(0.1f)).Boost(0.1f);
finalPredicate = finalPredicate.Or(isPagePredicate.Boost(9f)).Boost(9f);
return finalPredicate;
}
As you can see I've put boosting in multiple places (I've tried it both within the predicate and outside the predicate separately as well as together) but none of it is getting applied in the query:
?q=(((c_is_asset_b:(True)^0.01 AND (c_item_part_number_lower_s:(*systemkit*) OR c_title_lower_s:(*systemkit*) OR c_keywords_t:(*systemkit*) OR c_file_type_s:(*systemkit*) OR c_aggregated_search_fields_s:(*systemkit*) OR (c_item_part_number_lower_s:(*systemkit*) OR c_title_lower_s:(*systemkit*) OR c_keywords_t:(*systemkit*) OR c_file_type_s:(*systemkit*) OR c_aggregated_search_fields_s:(*systemkit*)) OR (title_t:(systemkit~100000))^2.5 OR (summary_t:(systemkit~100000))^1.5 OR _content:(systemkit~100000))) OR ((-c_is_asset_b:(True) *:*)^9999 AND (c_item_part_number_lower_s:(*systemkit*) OR c_title_lower_s:(*systemkit*) OR c_keywords_t:(*systemkit*) OR c_file_type_s:(*systemkit*) OR c_aggregated_search_fields_s:(*systemkit*) OR (c_item_part_number_lower_s:(*systemkit*) OR c_title_lower_s:(*systemkit*) OR c_keywords_t:(*systemkit*) OR c_file_type_s:(*systemkit*) OR c_aggregated_search_fields_s:(*systemkit*)) OR (title_t:(systemkit~100000))^2.5 OR (summary_t:(systemkit~100000))^1.5 OR _content:(systemkit~100000)))) AND _val_:("recip(ms(NOW, c_date_tdt), 3.16e-11, 100, 1.8)"))&start=0&rows=100&fq=((((-c_alltemplates_sm:(b906556e0ab94174952026c282933569) *:*) OR end_date_tdt:[2021-09-13T17:25:05.524Z TO *] OR start_date_tdt:[2021-09-13T17:25:05.524Z TO *]) AND c_searchable_b:(True)) AND _language:(en))&fq=_indexname:(sitecore_web_index)&facet=true&facet.field=c_locations_sm&f.c_locations_sm.facet.mincount=1&facet.field=c_architectures_sm&f.c_architectures_sm.facet.mincount=1&facet.field=c_topics_sm&f.c_topics_sm.facet.mincount=1&facet.field=c_content_type_s&f.c_content_type_s.facet.mincount=1&facet.field=c_file_type_s&f.c_file_type_s.facet.mincount=1&wt=xml
(there is some boosting in the Title and Summary, but not on the Is_Asset field that I'm trying to boost)
It turns out the issue was that boosting does not work on boolean fields. I changed the IsAsset field to a string ("true" or "false") and was then able to implement boosting
var isAssetPredicate = PredicateBuilder.True<T>().And(predicate).And(x => x.IsAssetStr.Equals("true").Boost(0.001f));
var isPagePredicate = PredicateBuilder.True<T>().And(predicate).And(x => x.IsAssetStr.Equals("false").Boost(999f));
Boosting SOLR queries does not work in sitecore versions 9.1-9.3. See below from sitecore website:
https://doc.sitecore.com/en/developers/93/platform-administration-and-architecture/search-result-boosting.html
Search result boosting works in Solr version 6.6 or earlier, but Solr changed the implementation in Solr 7, and later versions. Therefore, search result boosting is broken in Sitecore 9.1, 9.2, and 9.3.

.NET MVC returns error on Count()

I have a query:
foreach (var item in outcomeAreas)
{
var outcomeAreasCohorts = db.Cohorts
.Join(db.OutcomeArea, c => c.ID, oa => oa.CohortID, (c, oa) => new { c = c, oa = oa })
.Where(oa => oa.oa.OutcomeType.Contains(item.SubCategory.ToString()))
.Select(c => new { c.c.cohortID }).Distinct().ToList().Count();
allFilters.Add(new GetFilters { Category = item.Category, Identifier = item.Identifier, SubCategory = item.SubCategory, SubCategoryIdentifier = item.SubCategory.ToString().Replace(" ", "-"), TopLevel = "Key Cohort Outcomes", TotalNum = outcomeAreasCohorts.ToString() });
}
However, it is throwing an error on me.
Values of type 'collection[Edm.String(Nullable=True,DefaultValue=,MaxLength=,Unicode=,FixedLength=)]' can not be converted to string.
I tried searching, but am not finding this error. Thoughts?

Create c# lambda expression in a loop

How can I convert the following lambda expression for running a SharePoint query in CSOM to accept an array of parameters:
Current working version:
var listItems = list.GetItems(query);
clientContext.Load(listItems,
items => items.Include(
item => item["Title"],
item => item.RoleAssignments
)
);
clientContext.Load(list, l => l.Id, l => l.DefaultDisplayFormUrl);
clientContext.ExecuteQuery();
How I would like it to work:
var fields = new List<string>()
{
"Title",
"ID",
"DocumentOrder",
"FileRef"
};
var listItems = list.GetItems(query);
clientContext.Load(listItems,
items => items.Include(
item => fields,
item => item.RoleAssignments
)
);
clientContext.Load(list, l => l.Id, l => l.DefaultDisplayFormUrl);
clientContext.ExecuteQuery();
This throws an error, is it possible to pass in a dynamic list?
Though not ideal, I have been able to achieve what I am attempting to do by calling Load several times in a loop:
var fieldNames = new List<string>()
{
"Title",
"ID",
"DocumentOrder",
"FileRef"
};
var listItems = list.GetItems(query);
foreach (var fieldName in fieldNames)
{
clientContext.Load(listItems,
items => items.Include(
item => item[fieldName]
)
);
}
clientContext.ExecuteQuery();

Sharepoint Online Workflow restart error (Console Application)

I frequently get requests to restart a workflow for some of my list items. I have written an application which takes library name and list item name as an input. when i try to start a workflow (StartWorkflow method) i get this error Exception from HRESULT: 0x8102009B and no inner exception. Below is my code.
Sometimes the workflow may have already been started. In that case i need to cancel the workflow and then restart.
I did lot of search on this issue. Most of the resolution is for SP 2010. We are using SP Online
clientContext.Credentials = new SharePointOnlineCredentials(Userid, Password);
var web = clientContext.Web;
List list = web.Lists.GetByTitle("listname");
clientContext.Load(list);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='FileLeafRef' Descending='True'/></OrderBy><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + ListItemName + ".xml</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
ListItemCollection items = list.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
var id = items.Where(a => a.FieldValues["FileLeafRef"].ToString() == ListItemName + ".xml").Select(a => a.Id).FirstOrDefault();
Guid listItemID = (Guid)items.Where(a => a.FieldValues["FileLeafRef"].ToString() == ListItemName + ".xml").Select(a => a.FieldValues["GUID"]).FirstOrDefault();
WorkflowAssociationCollection wfaCollection = items.Where(a => a.FieldValues["FileLeafRef"].ToString() == ListItemName + ".xml")
.Select(a => a.ParentList.WorkflowAssociations).FirstOrDefault();
clientContext.Load(wfaCollection);
var workflowServiceManager = new WorkflowServicesManager(clientContext, web);
clientContext.Load(workflowServiceManager);
clientContext.ExecuteQuery();
InteropService workflowInteropService = workflowServiceManager.GetWorkflowInteropService();
clientContext.Load(workflowInteropService);
var wfaName = wfaCollection.Where(a => a.Enabled == true).Select(a => a.Name).FirstOrDefault();
Guid wfaId = wfaCollection.Where(a => a.Enabled == true).Select(a => a.Id).FirstOrDefault();
var initiationData = new Dictionary<string, object>();
ClientResult<Guid> resultGuid = workflowInteropService.StartWorkflow(wfaName, new Guid(), list.Id, listItemID, initiationData);
clientContext.ExecuteQuery();

linq compiled query error "parameteres cannot be sequences"

I've got the code below:
var catRoots = CatalogContext.CatalogRoots.Where(cr => cr.Visible);
var catChapter = CatalogContext.CatalogChapters.Where(cch => cch.Visible);
var catThemes = CatalogContext.CatalogThemes.Where(cth => cth.Visible);
var catCompanies = CatalogContext.CatalogCompanies.Where(cc => cc.Visible);
var catRelations = CatalogContext.CatalogCompanyThemeRelations.Where(cctr => cctr.Visible && cctr.OwnerVisible);
var regions = CatalogContext.Regions.AsQueryable();
var compChapters = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Chapter)
.Join(catChapter, cctr => cctr.ParentID, cch => cch.ID, (cctr, cch) => new { Relation = cctr, Chapter = cch })
.Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { CatalogRoot = cr, CatalogChapter = cch.Chapter, CatalogRelation = cch.Relation })
.Join(catCompanies, cr => cr.CatalogRelation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.CatalogRoot, Chapter = cr.CatalogChapter, Theme = default(CatalogTheme), Company = cc })
.Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc })
.GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
.Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });
var compThemes = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Theme)
.Join(catThemes, cctr => cctr.ParentID, cth => cth.ID, (cctr, cth) => new { Relation = cctr, Theme = cth })
.Join(catChapter, cth => cth.Theme.CatalogChapterID, cch => cch.ID, (cth, cch) => new { Relation = cth.Relation, Theme = cth.Theme, Chapter = cch })
.Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { Relation = cch.Relation, Theme = cch.Theme, Chapter = cch.Chapter, Root = cr })
.Join(catCompanies, cr => cr.Relation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.Root, Chapter = cr.Chapter, Theme = cr.Theme, Company = cc })
.Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc.Company })
.GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
.Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });
var source = compChapters.Union(compThemes);
var chapters = source.Select(r => new { Chapter = r.Chapter, Count = r.Count }).Cast<object>().Distinct();
public static Func<DataContext, IQueryable<object>, IEnumerable<object>> filteredFunc =
CompiledQuery.Compile<DataContext, IQueryable<object>, IEnumerable<object>>
(
(DataContext db, IQueryable<object> q) => q.Distinct().ToList()
);
filtredChapters = filteredFunc(CatalogContext, chapters);
I'm getting an error "parameteres cannot be sequences" when I run filteredFunc, which is weird, because "chapters" object is IQueryable, not IEnumerable, so why am I getting the error?
The code below works fine, but it is not good for me.
filtredChapters = chapters.Distinct().Cast<object>().ToList();
You cannot use compiled queries with an IEnumerable like this. The number of items in the enumeration can vary and so the query plan for the query will vary based on its size. Just remove the compiled query and use the function as is.

Categories

Resources