1

Would there be a difference in using the 'older' SharePoint ClientContext/CSOM based API against a site in SharePoint Online versus one that is a part of a Team?

I have a bit of code that works just fine when talking to a general SPO site.

using (var cc = new AuthenticationManager().GetACSAppOnlyContext(instance.url, instance.apiKey, instance.apiSecret))
{    
    Web web = cc.Web;
    var spTimezone = web.RegionalSettings.TimeZone;

    List eventsList = web.Lists.GetByTitle(instance.calendarTitle);
    CamlQuery query = CamlQuery.CreateAllItemsQuery(); 
    var list = web.Lists;

    ListItemCollection eventsItems = eventsList.GetItems(query);
    cc.Load(eventsItems);
    cc.ExecuteQuery();

    Logger.Info("Found " + eventsItems.Count + " items in " + instance.name);
}

But, if I run this against a site I am working on now, I get zero results. If I run against several sites I developed this code against (not tied to a Team) it works as expected and returns me a collection of list items.

I feel that my auth is working, as if I spell CalendarTitle wrong, I get an exception that List does not exist at URL.

I also can not seem to be able to see a list of all lists in the site, etc.

Is this due to a difference in this particular site? Why would I not be able to get any results, when it works fine on all of my other, non-Teams based sites?

5
  • I suggest that you post the entire code and the exact results. if the execution succeeds with 0 results I suppose either the list is empty or there are no results for your query criteria & specified account. remember to ensure that you have access rights to the items and to use viewscope if applicable, learn.microsoft.com/en-us/previous-versions/office/… Commented Feb 12 at 19:55
  • @TiagoDuarte added more code.
    – jmlumpkin
    Commented Feb 23 at 20:25
  • usually if it doesn't throw access denied but shows zero results, it means that your current account/token has access to query the site but not to the items themselves. I don't think it's a matter of site vs teams but more a matter of the access capabilities of your acs account, maybe it doesn't have access to teams sites. keep in mind that ACS is being retired: learn.microsoft.com/en-gb/sharepoint/dev/sp-add-ins/…. you can also run your code in a powershell console to try to understand better what's happening Commented Feb 27 at 14:56
  • Thank you. Unfortunately, I have found the documentation on both auth and talking to calendars to be very difficult to find even with the most modern integrations. Any pointers on good documentation to talk to a modern site, with modern auth, to get calendar data?
    – jmlumpkin
    Commented Feb 28 at 16:55
  • added an answer to try and cover everything. hope it helps! Commented Feb 28 at 19:16

1 Answer 1

0

Whether we're talking about C# or PowerShell, we need to authenticate in order to interact with SharePoint Online.

Right now, Azure Control Service (ACS) is still available but it is deprecated and scheduled for retirement. It should function until April 2nd, 2026, or until it is explicitely disabled via PowerShell Command. More on ACS here.

To connect to SharePoint Online via ACS I suggest this article,

https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/

Since ACS is old news, you may want to switch to the new Azure AD App.

The process is not easy but it is well documented in this article where Microsoft describes the step-by-step process of switching from ACS to Azure AD App,

https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins-modernize/from-acs-to-aad-apps

There's a lot of misconceptions around CSOM but for now it's still alive and kicking with no end in sight as you can see here.

That said, if what you're trying to accomplish doesn't need to be done via C#, you can switch to PowerShell and connect via PnP PowerShell, which sort of works as its own app, where you connect interactively and run your scripts, just like in C# but in a PowerShell script, no app needed. You'll obviously need access rights to the site and the ability to run custom scripts. PnP supports many authentication methods, including ACS and Azure AD Apps, but you can connect without them if you have access and are ok with the interactive prompt.

Example:

Install-Package -Name PnP.PowerShell
connect-pnponline -url https://contoso.com -interactive
$events = Get-PnPListItem -list Events

More details here,

https://www.sharepointdiary.com/2019/07/how-to-run-powershell-scripts-for-sharepoint-online.html

PowerShell PnP Module,

https://github.com/pnp/powershell

Other notes

  • Different PowerShell modules connect using a different context (Microsoft.Online.SharePoint.PowerShell connects you as an admin, PnP.PowerShell uses whatever credentials your provide, which can be your own)
  • Make sure the account or credential that you use to connect actually has access rights to the resources, otherwise you'll keep getting issues where the connection works but the data is not returned
  • Interestingly enough, you can use the PowerShell "app" to connect to a SharePoint site interactively via C# using the code below
  • The "right" way to authenticate to SharePoint Online going forward is probably the creation of the Azure AD App, so you should give it a try if it's an option for you. You may need an admin to help with consenting the rights if you're not one yourself

.

var authManager = PnP.Framework.AuthenticationManager.CreateWithInteractiveLogin("31359c7f-bd7e-475c-86db-fdb8c937548e");
ctx = authManager.GetContext(url);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.