Overcome List Threshold Limit on SharePoint

When you are using code to query a list especially those with a lot of columns or has more than 5k items, you will hit the error which contains the message of “list threshold limit”.

Here’s your regular code to get list item collection with CAML query.

List listAM = clientContext.Web.Lists.GetByTitle("Matrix");

StringBuilder sbQuery = new StringBuilder();
sbQuery.Append("<View>");
sbQuery.Append("<ViewFields>");
sbQuery.Append("<FieldRef Name='ID' />");
//you can add your own lines for more fields
sbQuery.Append("</ViewFields>");
sbQuery.Append("</View>");

CamlQuery caml = new CamlQuery();
caml.ViewXml = sbQuery.ToString();

ListItemCollection amItems = listAM.GetItems(caml);
clientContext.Load(amItems);
clientContext.ExecuteQuery();

foreach (ListItem am in amItems)
{
    //whatever you need to do with the item collection
}

The code above works perfectly fine until your list has more than 5k of items and you will hit the error of list threshold limit. How do you overcome it? SharePoint has a way to overcome it by utilising the ListItemCollectionPosition class that will retrieve the items in batch according to the conditions in the CAML query and number set for each retrieval.

Here’s the sample of updated code by adding the class of ListItemCollectionPosition and batch number. Once you can differentiate the additional code with regular code above, you can update your code with ease.

List listAM = clientContext.Web.Lists.GetByTitle("Matrix");

StringBuilder sbQuery = new StringBuilder();
sbQuery.Append("<View>");
sbQuery.Append("<ViewFields>");
sbQuery.Append("<FieldRef Name='ID' />");
//you can add your own lines for more fields
sbQuery.Append("</ViewFields>");
//sample to retrieve by 2000 each batch
sbQuery.Append("<RowLimit>2000</RowLimit>");
sbQuery.Append("</View>");

CamlQuery caml = new CamlQuery();
caml.ViewXml = sbQuery.ToString();
ListItemCollectionPosition licp = null;

while (true)
{
    caml.ListItemCollectionPosition = licp;

    ListItemCollection amItems = listAM.GetItems(caml);
    clientContext.Load(amItems);
    clientContext.ExecuteQuery();
    licp = amItems.ListItemCollectionPosition;

    foreach (ListItem am in amItems)
    {
       //whatever you need to do with the item collection
    }

    //this to keep looping until no more item to fetch
    if (licp == null)
    { break; }
}

Posted in Programming, SharePoint | Leave a comment

Accessing the Correct Control in a Page with Same Multiple Webparts Through Javascript

You have just make some control and Javascript in your webpart which looks similar as below:

Control
<div id=”btnX”></div>

Javascript

$('#btnX').click(function () {
$('#myTargetControl')...
..
});

Then you deployed it to your environment and place the webpart in a page. After adding the first webpart, you find that your script works as it intended. You might have some requirements to place the same webpart more than once in the same page. You proceed to add the same part a few more times in the same page.

When you tested it, you realised that the same script actually triggered multiple times (perhaps 5 times if you got 5 same webparts in a page)

How to solve it to make sure only the correct control is selected when you try trigger the script from a certain webpart or in this case the button that is in the same webpart as the targetted control?

Here’s the tricks for both control and javascript

Control – add the runat tag
<div id=”btnX” runat=”server”></div>

Javascript
– instead of calling the button ID directly, use ClientID as it will be unique for every webpart
– as long as the control and button are within same webpart and div, they will have same prefix which added with their own id. This case btnX and myTargetControl.
– the alert is put for testing purpose.

$('#<%=btnX.ClientID%>').click(function () {
var _thisX = $(this);
var _thisMyControl = _thisX.attr('id').replace('btnX', 'myTargetControl')
//alert(_thisX.attr('id') + "~~~" + _thisMyControl);
$('#'+_thisMyControl)...
..
});


Posted in Programming, SharePoint, Workarounds and Tricks | Leave a comment

Hide Sign In Link in SharePoint Master Page

If you got a SP site that allow anonymous access specifically internet site, you will see the link of Sign In by default which is available as control in master page. The one I’m working on is based on SP 2019 but it should be applicable to 2016 and 2013 as well for .html based master page.

Find the below code or something similar in your master page:

<!--SPM:<wssucw:Welcome id="IdOsloWelcome" runat="server" EnableViewState="false"/>-->

Add the following codes before and after the codes mentioned above:

<!--MS:<SharePoint:SPSecurityTrimmedControl runat="server" AuthenticationRestrictions="AuthenticatedUsersOnly">-->
<!--replace with code of welcome control-->
<!--ME:</SharePoint:SPSecurityTrimmedControl>-->

A bit explanation here: AuthenticationRestrictions will control what level of users will be able to view the item inside the block of codes. You can use it for various controls anywhere in the master page that you want to restrict. Means that you can use it for more than 1 place. You can use the following options for AuthenticationRestrictions:

  • AllUsers – for all type of users no matter authenticated or anonymous
  • AnonymousUsersOnly
  • AuthenticatedUsersOnly
Posted in Programming, SharePoint, SharePoint Designer, Styling | Leave a comment