SharePoint CSOM call failing to small list - c#

We have a Console application which makes SharePoint CSOM calls to a list in a SharePoint site in an intranet.
This call works with a list with 1000+ records. However this call is failing when we deployed the Console application in a different machine in the intranet.
It cannot be MaxReceivedMessageSize or MaxParseMessageSize because it works elsewhere.
Is there a machine specific setting that needs to be taken care?
The code looks like this:
using (var clientContext = new ClientContext(SharePointSiteUrl))
{
clientContext.Credentials = AuthenticationHelper.GetNetworkCredential();
var list = clientContext.Web.Lists.GetByTitle(RequestsList);
var requestIdField = list.Fields.GetByInternalNameOrTitle("RequestID");
var query = CamlQuery.CreateAllItemsQuery();
var items = list.GetItems(query);
clientContext.Load(requestIdField);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (var request in requests)
{
var itemCreateInfo = new ListItemCreationInformation();
var newItem = list.AddItem(itemCreateInfo);
newItem[requestIdField.InternalName] = request.RequestID;
newItem.Update();
}
clientContext.ExecuteQuery();
result = true;
}
ERROR:
AppName: Unhandled Exception: Details: Message:The request message is too big. The server does not allow messages larger than 2097152 bytes.
StackTrace: 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()
at AppName.SharePoint.SharePointHelper.Method3(List`1 requests)
at AppName.Program.Method2()
at AppName.Program.Main(String[] args)
TargetSite:Void ProcessResponseStream(System.IO.Stream)
Source:Microsoft.SharePoint.Client.Runtime

SharePoint has its own limits for CSOM. Unfortunately, these limits cannot be configured in Central Administration and also cannot be set using CSOM for obvious reasons.
When googling for the issue, mostly a solution is given by setting the ClientRequestServiceSettings.MaxReceivedMessageSize property to the desired size.
Call the following PowerShell script from SharePoint Management Shell :
$ws = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$ws.ClientRequestServiceSettings.MaxReceivedMessageSize = 209715200
$ws.Update()
This will set the limit to 200 MB.
However, in SharePoint 2013 Microsoft apparently added another configuration setting to also limit the amount of data which the server shall process from a CSOM request (Why anyone would configure this one differently is beyond me...). After reading a very, very long SharePoint Log file and crawling through some disassembled SharePoint server code, I found that this parameter can be set via the property ClientRequestServiceSettings.MaxParseMessageSize.
We are now using the following script with SharePoint 2013 and it works great:
$ws = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$ws.ClientRequestServiceSettings.MaxReceivedMessageSize = 209715200
$ws.ClientRequestServiceSettings.MaxParseMessageSize = 209715200
$ws.Update()

Related

Show more than 5000 records from CRM in C#

I am trying to understand how FetchXml works (or any other method) because I want to retrieve and process more than the limit of 5000 records that CRM returns on the api call below.
My base url looks like this: http://crm.domain.com:1234/api/data/v8.0/
the resource is: emails
and query options are: $top=50000&$filter=description ne null and not contains(sender, '#not-interesting.com')
I'm trying to copy the code from
https://learn.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327917(v=crm.8)?redirectedfrom=MSDN
but I'm having issues with creating the OrganizationServiceProxy object like this:
var creds = new ClientCredentials();
creds.UserName.UserName = CrmApiUsername;
creds.UserName.Password = CrmApiPassword;
using (var _serviceProxy = new OrganizationServiceProxy(
new Uri(CrmApiBaseAddress), null, creds, null))
{
// This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes(); // ...
I'm getting an error:
Metadata contains a reference that cannot be resolved: 'http://crm.domain.com:1234/data/v8.0/?wsdl&sdkversion=90'.'
WebException: The remote server returned an error: (404) Not Found.
You mixed up these two:
Web API - which was available after v8.0
2011 endpoint - which is deprecated now but was available for really long time
Read more
When you use web api the url will be like: https://yourcrm.crm#.dynamics.com/api/data/v8.0/
In case of Organization Service Proxy still the 2011 endpoint: https://yourcrm.crm.dynamics.com/XRMServices/2011/Organization.svc
For breaking the 5000 records limitation & getting more than 5k records, the pagination concept has to be used. How to fetch more than 5000 entities from CRM
For more ideas, I have answered in this SO thread about other options.

Partner STS returned a wst:RequestFailed error

I have a C# application that is accessing SharePoint online lists via the SharePoint CSOM SDK.
The below mentioned code randomly fails and throws the above the Partner STS error.
I have tried using the Async method call and setting DisableReturnValueCache to true on the client context.
var o365Credentials = new SharePointOnlineCredentials(SystemConfiguration.Sharepoint.AuthUser, o365SecurePassword);
var o365Context = new ClientContext(SystemConfiguration.Sharepoint.Url);
o365Context.Credentials = o365Credentials;
o365Context.DisableReturnValueCache = true;
// and while accessing list items :
CamlQuery queryList = CamlQuery.CreateAllItemsQuery();
ListItemCollection listItems = list.GetItems(queryList);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
context.Load(listItems, items => items.Include(
item => item.FieldValuesAsText,
item => item.DisplayName)
);
if (context.HasPendingRequest)
{
context.ExecuteQuery();
}
I expect the code to consistently fetch List items without random failures.
Please help! Thanks
So due to lack of response on this question, and any knowledge on GitHub or Other Microsoft Forums, I finally moved from Using SharePoint-CSOM to Microsoft Graph API for managing SharePoint lists.
And in my opinion, I have seen a significant improvement in performance and now my connection to Graph can be Singleton with regular Token Refreshes.
Thanks!

SharePoint 2013: Why getting 500 error while creating clientContext?

I want to create a IIS webservice which has to write list items to SharePoint on Premise.
I want to use CSOM and try to create a ClientContext.
public string AddListItem()
string result = string.Empty;
string siteUrl = "https://serverUrl";
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new NetworkCredential("User", "Password", "Domain");
List list = context.Web.Lists.GetByTitle("Test");
context.Load(list);
context.ExecuteQuery();
}
return result;
}
While executing, I get an error at context.ExecuteQuery();
System.Net.WebException: 'The remote server returned an error: (500) Internal Server Error.'
In the Event Log, I see following error:
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/41028758
Exception: System.ServiceModel.ServiceActivationException: The service '/_vti_bin/client.svc' cannot be activated due to an exception during compilation. The exception message is: Operation is not valid due to the current state of the object.. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.SharePoint.Administration.SPWebApplication.get_Context()
................................
In debugging, I also see after creating the ClientContext and before context.ExecuteQuery(); following error at some properties of ClientContext, e.g.:
ServerLibraryVersion = 'context.ServerLibraryVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'
Your code seems fine for on-prem SharePoint. I think You should check some settings on the farm that my be the cause of that.
Please check the services on farm server if the IIS Admin Service is on
also on SharePoint CA check the user profile service and the claims to windows token service (both should be on)
... sorry for the lang :)... usually I have access to SharePoint in PL language, but I tried to translate the most important stuff to ang.
Please also check if on IIS the app pools that You try to access are working correctly. I suppose yes, otherwise You would have a lot of other errors, but it's always better to check.
Use CSOM, what to use WCF is not clear:
ClientContextctx = newClientContext("http://gowtham.sharepoint.com");
List oList = ctx.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformationitemCreateInfo = newListItemCreationInformation();
ListItemnewItem = oList.AddItem(itemCreateInfo);
newItem["Title"] = "Test Item!";
newItem["Body"] = "welcome to Gowtham Blog";
newItem.Update();
context.ExecuteQuery();
Try below code
using (ClientContext clientContext = new ClientContext(ServerURL))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials(your_mail_ID, securePassword);
Web web = clientContext.Web;
var list = web.Lists.GetByTitle("your_list_name");
clientContext.Load(list.RootFolder);
clientContext.ExecuteQuery();
}
Check if the "SharePoint Web Servies Root" application pool was stopped or the "SharePoint Web Services" web application not start in IIS.
Go to IIS Application Pools, find the "SecurityTokenServiceApplicationPool" and click "Advanced Settings" from the action panel, then Scroll to "ProcessModel" section and change the Identity to your SharePoint Farm Account and do IISRESET.
And create a console application with the CSOM C# code to check if it works.
FWIW - I had exactly the same error - CSOM local mode to on-prem SP2016 with a 500 error requesting the list by title.
I had just applied a 10/2019 SharePoint update, but hadn't gone through Product Configuration Wizard and the prompts in Central Admin. Once I did that, CSOM requests worked again.

TF400813: Resource not available for anonymous access. Client authentication required

I am working on the CodedUI Test Automation project. i am developing a framework in which i am trying to access Test Cases in VSTS through RestAPI. I have worked on an MVC application previously in which i did the same thing to pull data from VSTS using RestAPI.
Now the problem is i am not able to access the VSTS. Everytime i am trying to access the VSTS, i got the exception TF400813: Resource not available for anonymous access. Client authentication required.
I am using the same PAT token. I have all the required access on my team project. I am able to access all work items in my project from browser. I have tried all the option mentioned in below thread but still its not working.
Client authentication error when starting Visual Studio 2015.3Any leads will be appreciated.Below is my code to get data from VSTS:
public static List<WorkItem> GetWorkItemsWithSpecificFields(IEnumerable<int> ids)
{
var collectionUri = "https://<name>.visualstudio.com";
var fields = new string[] {
"System.Id",
"System.Title",
"System.WorkItemType",
"Microsoft.VSTS.Scheduling.RemainingWork"
};
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(new Uri(collectionUri), new VssBasicCredential("", System.Configuration.ConfigurationManager.AppSettings["PATToken"])))
{
// Exception is coming on below line
List<WorkItem> results = workItemTrackingHttpClient.GetWorkItemsAsync(ids, fields).Result;
return results;
}
}

Programatically access TFS account to get project names and work items

I've been trying to access a TFS account (programatically using the TFS SDK) in order to get information such as the project names, work item names and developpment times for each work item.
I wasn't able to get project or work item information, but I was able to authenticate and access TFS using the class TfsTeamProjectCollection.
However, using the class TfsConfigurationServer, I was never able to authenticate to the server. This is a pity because the majority of examples I've seen in the web use this TfsConfigurationServer class.
The code that allows me to access TFS:
// Connect to Team Foundation Server.
NetworkCredential netCred = new NetworkCredential("myEmail#email.com", "myPassword");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://tfs.gfi.pt:4430/tfs/gfi/"),tfsCred);
tpc.Authenticate(); // This one works, and when I enter tpc I can see I am correctly signed-in in TFS (I can for instance check my full name)
ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(
I have 2 questions:
What is the difference between the TfsConfigurationServer and the TfsTeamProjectCollection? I know the first leads me to the Server-Level and the second to the Collection-level, but, what is the server-level and collection-level?
Why am I unable to get the projectCollection when I am perfectly capable of signing-in (why does the last line returns null when I am sure I have 2 projects on TFS)?
Regarding question #1, you can find some whys in Introducing the TfsConnection, TfsConfigurationServer and TfsTeamProjectCollection Classes. Shortly, you can have many user databases, i.e. Collection, managed by a single TFS instance, i.e. the Server.
I just found out what the problem was. I just needed to replace the last line:
ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(
By:
// Get the catalog of team project collections
ReadOnlyCollection<CatalogNode> collectionNodes = tpc.CatalogNode.QueryChildren(
new [] { CatalogResourceTypes.TeamProject},
false, CatalogQueryOptions.None);
// List the team project collections
foreach (CatalogNode collectionNode in collectionNodes)
{
Console.WriteLine(collectionNode.Resource.DisplayName);
}

Categories