My event receiver to create pages automatically only creates one page. How can i debug whats going on with my code?

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/114848

Question

My code for my feature's event receiver is as shown below. I need it to create all 3 pages test1,test2,test3.aspx but it only creates test1.aspx in the folder SitePages.

What could be wrong in this code?

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        string template = "spstd1.aspx";

        string[] pages = { "test1.aspx", "test2.aspx", "test3.aspx" };

        foreach (string s in pages)
        {
            try
            {
                using (SPSite site = new SPSite("http://labenv/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string newFilename = s;
                        string templateFilename = template;

                        string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
                        FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);
                        SPFolder libraryFolder = web.GetFolder("SitePages");
                        SPFileCollection files = libraryFolder.Files;
                        SPFile newFile = files.Add(newFilename, stream);


                        if (newFile.CheckOutType != SPFile.SPCheckOutType.None)
                        {
                            newFile.CheckIn("CheckedIn");
                        }
                    }
                }
            }
            catch
            {
                //Means page already exists, in this case ignore
            }
        }

    }
Was it helpful?

Solution

To debug your application you have to attach VS to the process w3wp.exe
You can also use the event log, to log some messages.

Instead of catching the exception when the file exists, try to use:

  string fileUrl = string.Format("{0}/{1}/{2}", web.Url, "SitePages", newFileName);
  SPFile file = web.GetFile(fileUrl);
  if (!file.Exists) //add

Debug WSP SharePoint

OTHER TIPS

The debug tips from Nk SP and Hardik me helped me find the problem. The hive process was already in use so at the second attempt the debugger showed that the process was already in use. I changed the code to this and it worked

using (SPWeb web = site.OpenWeb())
            {
                string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
                string templateFilename = template;
                FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);

                    foreach (string s in pages)
                    {
                        try
                        {
                            string newFilename = s;

                            SPFolder libraryFolder = web.GetFolder("SitePages");
                            SPFileCollection files = libraryFolder.Files;
                            SPFile newFile = files.Add(newFilename, stream);

                            if (newFile.CheckOutType != SPFile.SPCheckOutType.None)
                            {
                                newFile.CheckIn("CheckedIn");
                            }
                        }
                       catch
                       {
                            //Means page already exists, in this case ignore
                       }
                    }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top