Beispiel #1
0
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            try
            {
                SPUtility.ValidateFormDigest();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(Settings.SiteURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            string valueInternalName =
                                _listWorker.GetFieldInternalName(web, properties.ListId, Settings.ValueFieldName);
                            string dateInternalName =
                                _listWorker.GetFieldInternalName(web, properties.ListId, Settings.DateFieldName);
                            string userInternalName =
                                _listWorker.GetFieldInternalName(web, properties.ListId, Settings.UserFieldName);

                            properties.AfterProperties[valueInternalName] = _statisticValues.getValue(web, properties);

                            base.ItemAdding(properties);
                        }
                    }
                });
            } catch (Exception e)
            {
                Logger.LogException(e);
            }
        }
        public List <StatisticsItem> GetUserStatistics(string userId)
        {
            List <StatisticsItem> result = new List <StatisticsItem>();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(Settings.SiteURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.GetList("/Lists/" + Settings.ListName);

                            string internalUserField =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.UserFieldName);
                            string internalDateField =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.DateFieldName);
                            string internalValueField =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.ValueFieldName);

                            SPQuery query = new SPQuery();
                            query.Query   = string.Concat(
                                "<Where><Eq>",
                                "<FieldRef Name='" + internalUserField + "' LookupId='True'/>",
                                "<Value Type='User'>" + userId + "</Value>",
                                "</Eq></Where>",
                                "<OrderBy>",
                                "<FieldRef Name='" + internalDateField + "' Ascending='True' />",
                                "</OrderBy>"
                                );

                            SPListItemCollection items = list.GetItems(query);

                            foreach (SPListItem e in items)
                            {
                                result.Add(new StatisticsItem
                                {
                                    Date  = e[internalDateField].ToString(),
                                    User  = e[internalUserField].ToString(),
                                    Value = e[internalValueField].ToString()
                                });
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
            return(result);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!numberRequireValidator.IsValid && !dateCompareValidator.IsValid)
                {
                    throw new Exception("Input data validation failed");
                }

                SPWeb oldContext = SPContext.Current.Web;

                SPUtility.ValidateFormDigest();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(Settings.SiteURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list         = web.GetList("/Lists/" + Settings.ListName);
                            SPListItem listItem = list.AddItem();

                            string valueInternalName =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.ValueFieldName);

                            string dateInternalName =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.DateFieldName);

                            string userInternalName =
                                _listWorker.GetFieldInternalName(web, list.ID, Settings.UserFieldName);

                            listItem[valueInternalName] = txtNumber.Text;
                            listItem[dateInternalName]  = dtcDate.SelectedDate;
                            listItem[userInternalName]  = _statisticValues.getUser(oldContext);
                            listItem.Update();

                            UriBuilder uriBuilder = new UriBuilder(Page.Request.Url.OriginalString);
                            var query             = HttpUtility.ParseQueryString(uriBuilder.Query);
                            query["success"]      = "1";
                            query["message"]      = "Элемент списка успешно создан";
                            uriBuilder.Query      = query.ToString();

                            Response.Redirect(uriBuilder.ToString());
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }