简体   繁体   中英

Sharepoint using event receiver

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. 在此处输入图片说明

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM