none
Creating Alerts on subfolders programmically RRS feed

  • Question

  • I get this error when I try to add/create an alert on a subfolder

    ERROR:

    The page you selected contains a list that does not exist. It may have been deleted by another user

    _____________________________________________________________

    The subfolder does exist. What is wrong.

    _____________________________________________________________

    Dim user As SPUser = webSite.AllUsers("domain\user")

    For Each web As SPWeb In siteCollection

    web.AllowUnsafeUpdates = True

    Dim docLibName AsString = "Shared Documents"

    Dim docLibFolder As SPFolder = Nothing

    docLibFolder = web.Folders(docLibName)

    Dim folder As SPFolder = web.GetFolder(http://appWeb/sites/myCollection/wssSiteXXX/Shared Documents/Appraisal Documents)

    user.Alerts.Add(folder.Item, SPEventType.All,SPAlertFrequency.Immediate)

    user.Update()

    Tuesday, November 1, 2011 4:17 PM

Answers

  •  

    Your solution did work. 

    It was working earlier but I did not know it because I did not see the see alert in my list of alerts for the site.

    However, what I found was that the alerts was created.  I can see the alert in the 'ImmedSubscriptions' table BUT the reason that the alert did not appear in my list of alerts is because the alert had the USERID of the network service account that I used to create the folder many months earlier.

    So, just to test the theory, I changed the USERID of the alert in table 'ImmedSubscriptions' to my USERID and then the alert appeared in my list of alerts.

    Now, I am running this piece of code with elevated privileges (and I tried it without elevated privileges), the alert record still captures the wrong USERID, and thus does not appear in my alert list. 

    The USER.ID field is readonly in the above used code.

     Can I use a separate Alert object, with the above solution, and manually set properties as needed like userid, alert title, etc.,

    then set the Alert object to the user object?

    How can I resolve this USERID issue?

    But thank you for the initial solution it worked !!!!!!!!!

     

     


    Leon



    • Edited by tun2 Wednesday, November 9, 2011 2:02 PM
    • Marked as answer by tun2 Wednesday, November 9, 2011 4:49 PM
    Wednesday, November 9, 2011 1:57 PM

All replies

  • try to use server-relative URL of the folder which is passed to the web.GetFolder() method
    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    Tuesday, November 1, 2011 7:04 PM
  •  

    It got pass this statement :

    user.Alerts.Add(folder.Item,SPEventType.All,SPAlertFrequency.Immediate)

     

    But I got the below error at the user.Update() statement.

     System.InvalidOperationException: Operation is not valid due to the current state of the object. at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context) at Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context) at Microsoft.SharePoint.SPContext.get_Current() at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) at Microsoft.SharePoint.SPListItem.Update() at Microsoft.SharePoint.SPUser.Update() at frmAlerts.AddFolderAlertsNew2() in D:\Web\projectLapDivisions\frmAlerts.aspx.vb:line 216


    Leon

    • Edited by tun2 Tuesday, November 1, 2011 7:25 PM
    Tuesday, November 1, 2011 7:17 PM
  • solution of the last problem depends on where you execute your code: where you have not null HttpConext.Current or where HttpContext.Current is null.

    For the first case I wrote about possible fix it here: Fix “Operation is not valid due to the current state of the object” error. Briefly solution is the following:

     

    var ctx = HttpContext.Current;
    try
    {
        HttpContext.Current = null;
        // write your code here
    }
    finally
    {
        HttpContext.Current = ctx;
    }
    

    For second case check this link: Faking SPContext.

     


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    Tuesday, November 1, 2011 7:53 PM
  •  

    I got the same error in the first case.

     

    I am trying to work with  the example in the second case.


    Leon
    Tuesday, November 1, 2011 8:40 PM
  •  

    Update - 11/2/2011 - 12:00pm

    I got your solution to work - i.e., the first case solution. I did not get an error. However, an alert was not created.

    Here is my code below:

    ______________________________

    Dim ctx = HttpContext.Current

    Try

    HttpContext.Current = Nothing

    Dim MainRootHomeSite As SPSite = Nothing

    Dim webSite As SPWeb = Nothing

    'Get Main WSS Site Collection.

    MainRootHomeSite = New SPSite(ConfigurationManager.AppSettings(WSS_APPLICATION_HOME_SITE))

    'Get Website.

    webSite = MainRootHomeSite.OpenWeb("webSite")

    'Allow updates to the WSS content database.

    webSite.AllowUnsafeUpdates = True

    'Get user Name.

    Dim user As SPUser = webSite.AllUsers("domain\user")

    Dim docLibName As String = "Shared Documents"

    Dim docLibFolder As SPFolder = Nothing

    docLibFolder = webSite.Folders(docLibName)

    Dim urlString As String = Nothing

    urlString = "http://appWeb/sites/siteCollection/webSite/Shared Documents/MANAGEMENT"

    Dim folder As SPFolder = webSite.GetFolder(urlString)

    user.Alerts.Add(folder.Item, SPEventType.All, SPAlertFrequency.Immediate)

    user.Update()

    Finally

    HttpContext.Current = ctx

    End Try

    Catch ex As Exception

    Response.Write(ex)

    End Try

    ______________________________

    I tried the urlString like

    urlString = http://appWeb/sites/siteCollection/webSite/Shared Documents/MANAGEMENT

    and like

    urlString = "/sites/siteCollection/webSite/Shared Documents/MANAGEMENT"

    However, neither way created the alert.

     


    Leon
    • Marked as answer by tun2 Wednesday, November 9, 2011 4:50 PM
    • Unmarked as answer by tun2 Wednesday, November 9, 2011 4:50 PM
    Wednesday, November 2, 2011 4:23 PM
  • are you sure that alert was not created? Alerts are sent by timer job, i.e. not immediately after event was triggered. You can decrease interval for alerts sending using this technique: How to set the Interval for Alerts in SharePoint 2007. If you set alerts interval to every 1 minute and they won't be sent, check Sharepoint logs (in 12/logs folder). Very often the reason of problems with alerts is in security. Check that your user have enough permissions to access the content which caused alert - login using user credentials and try to access the content by URL.

    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    Monday, November 7, 2011 9:58 PM
  •  

     Okay, I will check and try that.


    Leon
    Tuesday, November 8, 2011 2:55 PM
  •  

    Your solution did work. 

    It was working earlier but I did not know it because I did not see the see alert in my list of alerts for the site.

    However, what I found was that the alerts was created.  I can see the alert in the 'ImmedSubscriptions' table BUT the reason that the alert did not appear in my list of alerts is because the alert had the USERID of the network service account that I used to create the folder many months earlier.

    So, just to test the theory, I changed the USERID of the alert in table 'ImmedSubscriptions' to my USERID and then the alert appeared in my list of alerts.

    Now, I am running this piece of code with elevated privileges (and I tried it without elevated privileges), the alert record still captures the wrong USERID, and thus does not appear in my alert list. 

    The USER.ID field is readonly in the above used code.

     Can I use a separate Alert object, with the above solution, and manually set properties as needed like userid, alert title, etc.,

    then set the Alert object to the user object?

    How can I resolve this USERID issue?

    But thank you for the initial solution it worked !!!!!!!!!

     

     


    Leon



    • Edited by tun2 Wednesday, November 9, 2011 2:02 PM
    • Marked as answer by tun2 Wednesday, November 9, 2011 4:49 PM
    Wednesday, November 9, 2011 1:57 PM
  •  

    I tried this piece of code:

    ____________________________________________________________________

    ....

    Dim urlStringAsString =Nothing

    urlString ="http://appWeb/sites/siteCollection/webSite/Shared Documents/MANAGEMENT"

    Dim folderAs SPFolder = webSite.GetFolder(urlString)

    user.Alerts.Add(folder.Item, SPEventType.All, SPAlertFrequency.Immediate)

    alert = webSite.Alerts.Add()

    alert.AlertType = SPAlertType.List

    alert.AlertFrequency = SPAlertFrequency.Immediate

    alert.List = webSite.Lists("Shared Documents")alert.User = user

    alert.Update(True)

    _______________________________________

    but it only create two alerts. It did not correctly added the user.id.


    Leon
    Wednesday, November 9, 2011 5:01 PM