0

I have the following code to read to SUM of Total, PaidAmount, and ForgivenAmount from SharePoint 2019 Library named Invoice.

public void GetTotalsForClient(int clientID, DateTime startDate)
    {
        using (SPSite site = new SPSite(Constants.SiteName))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList invoiceList = web.Lists.TryGetList("Invoice");

                if (invoiceList != null)
                {
                    string queryViewXml = $@"
                <View>
                    <RowLimit>1</RowLimit>
                    <Query>
                        <Where>
                            <Eq>
                                <FieldRef Name='{Constants.Invoice_Field_ClientID}' LookupId='True' />
                                <Value Type='Lookup'>{clientID}</Value>
                            </Eq>
                        </Where>
                        <Aggregations Value='On'>
                            <FieldRef Name='InvoiceTotal' Type='SUM' Alias='TotalSum' />
                            <FieldRef Name='AmountPaid' Type='SUM' Alias='PaidAmountSum' />
                            <FieldRef Name='ForgivenAmount' Type='SUM' Alias='ForgivenAmountSum' />
                        </Aggregations>
                    </Query>
                    <ViewFields>
                        <FieldRef Name='TotalSum' />
                        <FieldRef Name='PaidAmountSum' />
                        <FieldRef Name='ForgivenAmountSum' />
                    </ViewFields>
                </View>";

                    SPQuery query = new SPQuery
                    {
                        ViewXml = queryViewXml
                    };
                    query.ViewFieldsOnly = true;
                    var items = invoiceList.GetItems(query);

                    Console.WriteLine($"Number of items returned: {items.Count}");

                    if (items.Count > 0)
                    {
                        var item = items[0];

                        double total = 0;
                        double paidAmount = 0;
                        double forgivenAmount = 0;

                        if (item["TotalSum"] is double totalSum)
                        {
                            total = totalSum;
                        }

                        if (item["PaidAmountSum"] is double paidAmountSum)
                        {
                            paidAmount = paidAmountSum;
                        }

                        if (item["ForgivenAmountSum"] is double forgivenAmountSum)
                        {
                            forgivenAmount = forgivenAmountSum;
                        }

                        Console.WriteLine($"Total: {total}");
                        Console.WriteLine($"Paid Amount: {paidAmount}");
                        Console.WriteLine($"Forgiven Amount: {forgivenAmount}");
                    }
                }
            }
        }
    }

However, the result is not as expected and it doesn't return SUM of the columns specified in Aggregations section. It throws an error saying that columns 'TotalSum', 'PaidAmountSum' and 'ForgivenAmountSum' are not found when trying to read values from result item.

Please help me with this issue and guide me to correct the problem and achieving the desired results. Apologies for any mistakes in question. Please let me know if any additional information would help.

Nikhil Chavan
  • 1,685
  • 2
  • 20
  • 34

0 Answers0