Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

http://screencloud.net/v/2xVc

But when manager Is created a element it's generate me error.

How do If on development If i Can delete element?

Please help. I have got this sort of code:

    for (int intIndex = elementyDoUsuniecia.Count - 1; intIndex > -1; intIndex--)
                        {
                            if (Convert.ToDateTime(elementyDoUsuniecia[intIndex]["Dzien"]).ToShortDateString() == Convert.ToDateTime(TextBox4.Text).ToShortDateString() && elementyDoUsuniecia[intIndex]["Pracownik"].ToString() == Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName.ToLower())
                            {
                                elementyDoUsuniecia.Delete(intIndex);
                                Label3.Text = "";
                                Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania został skasowany przez Ciebie z Twojego rozliczenia czasu pracy.</span>";
                                var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                            }

Here is my all deleting button:

      protected void Button3_Click(object sender, EventArgs e)
            {
                if (TextBox4.Text != "")
                {
                    if (!(Convert.ToDateTime(TextBox4.Text).ToUniversalTime().AddHours(1.0) >= DateTime.Now.ToLocalTime().AddMonths(-2).AddDays(-1.0) && Convert.ToDateTime(TextBox4.Text).ToUniversalTime().AddHours(2.0) <= DateTime.Now.ToLocalTime()))
                    {
                        Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Wskazana data nie znajduje się w 2 miesięcznym zakresie</span>";
                        var urlUnableDeletingARecord = SPContext.Current.Web.Url + "/_layouts/Add4/UnableDeletingARecord.aspx";
                        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog22('" + urlUnableDeletingARecord + "', 501, 108, 'Niemożiwe skasowanie dnia projektowego w wybranym dniu.', null);", true);
                        return;
                    }
                    SPWeb witryna = SPContext.Current.Web;
                    SPList oList = witryna.Lists["Szczegoly"];
                    // oList.EnableThrottling = false;
                    // SPListItemCollection elementyDoUsuniecia = oList.Items;
                    string FullQuery = "<Where><And><Eq><FieldRef Name='{0}' /><Value Type='DateTime'>{1}</Value></Eq><Eq><FieldRef Name='Pracownik' /><Value Type='Text'>{2}</Value></Eq></And></Where>";
                    string dateISO = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(TextBox4.Text).ToLocalTime().AddHours(-1.0));
                    var query1 = new SPQuery();
                    if (sprawdzanieUserow())
                    {
                        query1.Query = String.Format(FullQuery, "Dzien", dateISO, DropDownList1.SelectedItem.Text);
                    }
                    else
                    {
                        query1.Query = String.Format(FullQuery, "Dzien", dateISO, Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName.ToLower());
                    }
                    query1.ViewFields = "<FieldRef Name='Dzien' /><FieldRef Name='Pracownik' />";
                    SPListItemCollection elementyDoUsuniecia = witryna.Lists["Szczegoly"].GetItems(query1);
                    if (sprawdzanieUserow())
                    {
                        var nbrElements = elementyDoUsuniecia.Count;
                        for (int intIndex = elementyDoUsuniecia.Count - 1; intIndex > -1; intIndex--)
                        {
                            if (Convert.ToDateTime(elementyDoUsuniecia[intIndex]["Dzien"]).ToShortDateString() == Convert.ToDateTime(TextBox4.Text).ToShortDateString() && elementyDoUsuniecia[intIndex]["Pracownik"].ToString() == DropDownList1.SelectedValue)
                            {

                                elementyDoUsuniecia.Delete(intIndex);
                                Label3.Text = "";
                                Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania został skasowany przez Ciebie z Twojego rozliczenia czasu pracy.</span>";
                                var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                            }
                            else
                            {
                                Label3.Text = "";
                                Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania, nie istnieje na liście.</span>";
                                var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                            }
                        }
                        Calendar1_SelectionChanged(sender, e);
                    }
                    else
                    {
                        var nbrElements = elementyDoUsuniecia.Count;
                        for (int intIndex = elementyDoUsuniecia.Count - 1; intIndex > -1; intIndex--)
                        {
                            if (Convert.ToDateTime(elementyDoUsuniecia[intIndex]["Dzien"]).ToShortDateString() == Convert.ToDateTime(TextBox4.Text).ToShortDateString() && elementyDoUsuniecia[intIndex]["Pracownik"].ToString() == Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName.ToLower())
                            {
                                elementyDoUsuniecia.Delete(intIndex);
                                Label3.Text = "";
                                Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania został skasowany przez Ciebie z Twojego rozliczenia czasu pracy.</span>";
                                var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                            }

                            else
                            {
                                Label3.Text = "";
                                Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania, nie istnieje na liście.</span>";
                                var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                            }
                        }
                        Calendar1_SelectionChanged(sender, e);
                    }
                }
                else
                {
                    Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Nie wybrałeś daty dnia, który chcesz usunąć. Wybierz datę z jednego, bądz drugiego kalendarza.</span>";
                    var urlDeletingError = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingError.aspx";
                    ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog16('" + urlDeletingError + "', 501, 93, 'Data do skasowania nie została wybrana.', null);", true);
                }
            }

I want to make a massage when user haven't got a rights to delete element, or the element isn't exist.

But if I use else block of code nothing shown, any worring.

  if (Convert.ToDateTime(elementyDoUsuniecia[intIndex]["Dzien"]).ToShortDateString() == Convert.ToDateTime(TextBox4.Text).ToShortDateString() && elementyDoUsuniecia[intIndex]["Pracownik"].ToString() == Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName.ToLower())
                        {
                            elementyDoUsuniecia.Delete(intIndex);
                            Label3.Text = "";
                            Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania został skasowany przez Ciebie z Twojego rozliczenia czasu pracy.</span>";
                            var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                        }

                        else
                        {
                            Label3.Text = "";
                            Label2.Text = "<span style='color:red; font-size:13px; font-weight:bold;'>Dzień który wybrałeś do skasowania, nie istnieje na liście.</span>";
                            var urlDeletingTheRecord = SPContext.Current.Web.Url + "/_layouts/Add4/DeletingTheRecord.aspx";
                            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog3('" + urlDeletingTheRecord + "', 501, 150, 'Potwierdzenie skasowania rozliczenia czasu pracy w wybranym dniu.', null);", true);
                        }

And if i Use var nbrElements = elementyDoUsuniecia.Count; it's always show me 0 in nbrElements.

share|improve this question
add comment

1 Answer 1

You need to store elementyDoUsuniecia.Count in a parameter before you start the iteration (because otherwise Count will change with each new iteration).

Like this:

var nbrElements = elementyDoUsuniecia.Count;

for (int intIndex = nbrElements - 1; intIndex > -1; intIndex--)
{
    ...
}

This is a classic problem with iteration over an array while deleting items in it.

For handeling FileNotFoundException you can always do an try catch around the code that retreives the list item and catch the exception.

For checking if user has permissions to delete the item, you can use: yourListItem.DoesUserHavePermissions(SPBasePermissions.DeleteListItems) it returns true if user has right to delete the item.

share|improve this answer
    
But how use next Count to find out when to use a massage warring ? –  Grzegorz Z Apr 15 '13 at 9:24
    
What do you mean? intIndex is still accessible in all code in your for loop. Just make sure that you use the new variable nbrElements like: int intIndex = nbrElements -1 when starting the for loop. the you can use intIndex to know how many times you hav iterated –  Robert Lindgren Apr 15 '13 at 9:50
    
Look my last edit in my question. I addet same description what i mean :) –  Grzegorz Z Apr 15 '13 at 10:03
    
Still do not get what you mean. nbrElements will with my code be the number of items in the list before deleting anything. intIndex will show you witch item you are currently trying to delete, –  Robert Lindgren Apr 15 '13 at 10:08
    
And another thing, please do not change the whole question when getting more information, that makes previous answers look totally out of context –  Robert Lindgren Apr 15 '13 at 10:11
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.