0

I have a requirement from the client to show the percentage of the column value out of the sum of the column just like the picture below with out the widget. preferably I want to use JS link in sharepoint. please let me know if this is something we can accomplish with js link. enter image description here

1 Answers1

0

decided to do it with event receiver and would like to share the code if any one has similar requirement. event receiver make it easy to get the sum of the column and get the percentage for each row. see the code for ItemAdded event but you would want to add the same code for ItemUpdated and ItemDeleted event so that it will reflect the update accordingly.

    public override void ItemAdded(SPItemEventProperties properties)
    {

        base.ItemAdded(properties);
        string listName = "your list name goes here";
        using (SPWeb web = properties.OpenWeb())
        {
            if (web.Lists[listName] != null)
            {
                try
                {
                    SPList splist = web.Lists[listName];
                    int sum = 0;
                    foreach (SPListItem item in splist.Items)
                    {
                        sum = sum + Convert.ToInt32(item["Values"]);
                    }
                    //LoggingService

                    foreach (SPListItem item in splist.Items)
                    {
                        string percentage = string.Empty;
                        int percent = Convert.ToInt32(item["Values"]);/// Convert.ToInt32(sum);
                        double questient = (double)percent / sum;

                        percentage = questient.ToString("P1", CultureInfo.InvariantCulture);
                        item["Percent & widget"] = percentage;
                        this.EventFiringEnabled = false;
                        item.Update();
                        this.EventFiringEnabled = true;

                    }

                }
                catch (Exception ex)
                {

                    // LoggingService
                }

            } // end if
        }  // end using
    }