I'm new and I apologize if I'm doing something incorrectly. I need help on the class library I developed to be able to work on sharepoint on-prem. This library will be used from an ERP where users need to upload, search and download file from sharepoint. My DLL was working perfectly using Microsoft.Sharepoint.CLient and Microsoft.Sharepoint.CLient.Runtime assemblies with version 16.1.8810.1200. Now Microsoft has released a new version ( 16.1.24322.1200) and now my code doesn't work! When it try to run the clientcontext.executequery I receive an error The remote server returned an error: (401) Unauthorized.
I tried to change the autentication using Pnp.Framework and a certificate but it didn't work. I don't know what else to do. This is the part of my code (that was working), I hope you can give any idea on how to fix the issue, I really need to find a solution:
public static string GetFileListByMetadata(string SiteURL, string LibraryName,string User)
{
CheckIFStringParameterIsNullOrEmpty(SiteURL, "Site URL");
CheckIFStringParameterIsNullOrEmpty(LibraryName, "Library Name");
CheckIFStringParameterIsNullOrEmpty(User, "User");
CheckIfMetadataListIsInitialized();
CheckIfMetadataFilterListIsEmpty();
SortMetadataList();
bool IsAndConditionRequired = MetadataList_GetNumberOfFilter() > 1;
string MetadataFiledName = null;
string QueryFilter = @"<View Scope='RecursiveAll'><Query><Where> ";
if (IsAndConditionRequired)
{
QueryFilter = QueryFilter + "<And>";
}
foreach (MetaDataRecord mr in MetadataList)
{
if (MetadataFiledName == null)
{
QueryFilter = QueryFilter + "<In><FieldRef Name='" + mr.MetadataFieldName + "'/><Values>";
MetadataFiledName = mr.MetadataFieldName;
}
if (MetadataFiledName != mr.MetadataFieldName)
{
MetadataFiledName = mr.MetadataFieldName;
QueryFilter = QueryFilter + "</Values></In>";
QueryFilter = QueryFilter + "<In><FieldRef Name='" + mr.MetadataFieldName + "'/><Values>";
}
QueryFilter = QueryFilter + "<Value Type='Text'>" + mr.MetadataFieldValue + "</Value>";
}
QueryFilter = QueryFilter + "</Values></In>";
if (IsAndConditionRequired)
{
QueryFilter = QueryFilter + "</And>";
}
QueryFilter = QueryFilter + "</Where></Query></View>";
ClientContext context = new ClientContext(SiteURL);
Web web = context.Web;
List ItemList = web.Lists.GetByTitle(LibraryName);
CamlQuery query = new CamlQuery();
query.ViewXml = QueryFilter;
ListItemCollection Listitems = ItemList.GetItems(query);
context.Load(Listitems,
items => items.Include(
item => item.DisplayName,
item => item.Id,
item => item.File.Name,
item => item["EncodedAbsUrl"],
item => item["Modified"],
item => item["Author"],
item => item.File.ServerRelativeUrl
)
);
context.ExecuteQuery();
string JsonResponse = null;
List<object> Resultlist = new List<object>();
List<PathRecord> PathList = new List<PathRecord>();
foreach (ListItem ListItem in Listitems)
{
bool UserHasPermission = true;
PathRecord result = PathList.Find(x => x.url == ListItem.File.ServerRelativeUrl.Replace(ListItem.File.Name, ""));
if (result != null)
{
UserHasPermission = result.allowed;
}
else
{
bool Allowed = UserHasViewPermission(SiteURL, ListItem.File.ServerRelativeUrl.Replace(ListItem.File.Name, ""), User);
PathList.Add(new PathRecord(ListItem.File.ServerRelativeUrl.Replace(ListItem.File.Name, ""), Allowed));
UserHasPermission = Allowed;
}
if (UserHasPermission)
{
dynamic obj = new System.Dynamic.ExpandoObject();
obj.Name = ListItem.File.Name;
obj.ID = ListItem.Id;
obj.AbosulteURL = ListItem["EncodedAbsUrl"];
obj.RelativeURL = ListItem.File.ServerRelativeUrl;
obj.TimeLastModified = ListItem["Modified"];
Resultlist.Add(obj);
}
}
JsonResponse = JsonConvert.SerializeObject(Resultlist);
if (MetadataList != null)
{
MetadataList.Clear();
}
return JsonResponse;
}