private void IncreaseItemOrderNo(SPItemEventProperties properties, bool isUpdate)
        {
            if (properties.AfterProperties[Constants.ORDER_NUMBER_COLUMN] != null && !string.IsNullOrEmpty(properties.AfterProperties[Constants.ORDER_NUMBER_COLUMN].ToString()))
            {
                int orderNo = 0;
                int.TryParse(properties.AfterProperties[Constants.ORDER_NUMBER_COLUMN].ToString(), out orderNo);

                if (orderNo != 0)
                {
                    string strItemId = properties.ListItem == null ? "0" : properties.ListItemId.ToString();

                    string caml = string.Empty;
                    var expressionsAnd = new List<Expression<Func<SPListItem, bool>>>();

                    expressionsAnd.Add(x => ((int)x[Constants.ORDER_NUMBER_COLUMN]) >= orderNo);
                    expressionsAnd.Add(x => (x["ID"]) != (DataTypes.Counter)strItemId);

                    caml = Camlex.Query().WhereAll(expressionsAnd).OrderBy(x => x[Constants.ORDER_NUMBER_COLUMN] as Camlex.Asc).ToString();

                    SPQuery spQuery = new SPQuery();
                    spQuery.Query = caml;

                    SPList currentList = properties.List;
                    SPListItemCollection items = currentList.GetItems(spQuery);

                    foreach (SPListItem item in items)
                    {
                        orderNo++;

                        using (DisableItemEvent scope = new DisableItemEvent())
                        {
                            item[Constants.ORDER_NUMBER_COLUMN] = orderNo;
                            item.SystemUpdate(false);
                        }
                    }
                }
            }
            else
            {
                if (!isUpdate)
                    properties.AfterProperties[Constants.ORDER_NUMBER_COLUMN] = GetLatestItemOrderNo(properties) + 1;
            }
        }
        private void CreateList(SPListItem listItem, SPWeb web, ListIntanceDefinition definition, string UrlFieldName, out string listUrl)
        {
            listUrl = string.Empty;
            try
            {
                var template = web.ListTemplates.Cast<SPListTemplate>().Where(p => p.Name == definition.TemplateName).FirstOrDefault();
                if (template == null) return;
                string title = listItem.GetFormulaValue(definition.Title);
                string url = listItem.GetFormulaValue(definition.Url).Simplyfied();
                if (template.CategoryType == SPListCategoryType.CustomLists)
                    url = "Lists/" + url;

                var listId = web.Lists.Add(title, string.Empty, url, template.FeatureId.ToString(), template.Type_Client, "100", SPListTemplate.QuickLaunchOptions.Default);
               // Guid listId = web.Lists.Add(item.Title, item.Description, item.Url, template.FeatureId.ToString(), item.TemplateId, "100");

                SPList list = web.Lists[listId];
                list.ContentTypesEnabled = true;
                list.EnableVersioning = true;
                list.EnableModeration = true; //enable Content Approval
                foreach (var item in definition.ContentTypes)
                {
                    list.EnsureContentTypeInList(item);
                }

                 var urlValue = new SPFieldUrlValue();
                urlValue.Description = title;
                urlValue.Url = web.Url + "/" + url;

                using (DisableItemEvent disableItemEvent = new DisableItemEvent())
                {
                    listItem[UrlFieldName] = urlValue;
                    listItem.Update();
                }

                listUrl = urlValue.Url;
                list.Update();
            }
            catch (Exception ex)
            {
                Utility.LogError(ex.Message + ex.StackTrace, AIAPortalFeatures.Infrastructure);
                //throw;
            }
        }
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            var ct = properties.ListItem.ContentType;
            var setting = ct.GetCustomSettings<AutoCreationSettings>(AIAPortalFeatures.Infrastructure);
            var listItem = properties.ListItem;
            if (setting == null || !setting.RunOnCreated || !setting.EnableCreateList) return;
            if (setting.EnableCreateList)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    var url = listItem[setting.UrlFieldName].ToString();
                    var arr = url.Split(new char[] { ',' });
                    SPFieldUrlValue fieldValue = new SPFieldUrlValue(url);
                    fieldValue.Description = listItem.Title;
                    //fieldValue.Url = arr[1];

                    using (DisableItemEvent disableItemEvent = new DisableItemEvent())
                    {
                        listItem[setting.UrlFieldName] = fieldValue;
                        listItem.SystemUpdate();
                    }
                });
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            int orderNo = 0;
            int.TryParse(txtOrderNo.Text, out orderNo);

            if (orderNo != 0)
            {
                string listId = Request.QueryString["ListId"];
                string itemId = Request.QueryString["ID"];
                string sourceUrl = Request.QueryString["Source"];

                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                        {
                            web.AllowUnsafeUpdates = true;

                            try
                            {
                                SPList curList = web.Lists[new Guid(listId)];
                                SPListItem curItem = curList.Items.GetItemById(Convert.ToInt32(itemId));

                                string caml = string.Empty;
                                var expressionsAnd = new List<Expression<Func<SPListItem, bool>>>();

                                expressionsAnd.Add(x => ((int)x[Constants.ORDER_NUMBER_COLUMN]) >= orderNo);
                                expressionsAnd.Add(x => (x["ID"]) != (DataTypes.Counter)itemId);

                                caml = Camlex.Query().WhereAll(expressionsAnd).OrderBy(x => x[Constants.ORDER_NUMBER_COLUMN] as Camlex.Asc).ToString();

                                SPQuery spQuery = new SPQuery();
                                spQuery.Query = caml;

                                SPListItemCollection items = curList.GetItems(spQuery);

                                using (DisableItemEvent scope = new DisableItemEvent())
                                {
                                    curItem[Constants.ORDER_NUMBER_COLUMN] = orderNo;
                                    curItem.SystemUpdate(false);
                                }

                                foreach (SPListItem item in items)
                                {
                                    orderNo++;

                                    using (DisableItemEvent scope = new DisableItemEvent())
                                    {
                                        item[Constants.ORDER_NUMBER_COLUMN] = orderNo;
                                        item.SystemUpdate(false);
                                    }
                                }
                            }
                            catch (Exception ex) { }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                });
            }

            ClosePopup();
        }