locked
Object Model - Exercise RRS feed

  • Question

  • Hi, I am new to Sharepoint and solving an exercise http://img251.imageshack.us/i/image005fw.jpg/

    I know I have to use Sharepoint object model to solve this but I am confused when I open VS 2010 in selecting templates. I would like to deploy it as a webpart.

    Please guide me.

    Thanks.

    Saturday, April 23, 2011 1:36 PM

Answers

  • create a new visual web part, drag a button onto the design surface, double click the button, and add this code to the code behind. This will render text inside your web part. No other windows should pop up or anything like that. I don't know what i was looking at with your screenshot - why was there a big "stop editing" button at the center of the page? Was this web part placed on a normal web part page like home.aspx? anyway...

     

            protected void Button1_Click(object sender, EventArgs e)
            {
                 int i = 0;
                 var label = new Label();
                 Controls.Add(label);
                 label.Text = " You are about to start the project...";
                 using (SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName))
                 {
                     SPWebCollection collWebsites = oSiteCollection.AllWebs;
                     label = new Label();
                     label.Text = string.Format("Count: {0}",  collWebsites.Count);
                     Controls.Add(label);                
                     foreach (SPWeb oWebsite in collWebsites)
                     {
                         label = new Label();
                         label.Text = string.Format("Web site: {0}", oWebsite.Url);
                         Controls.Add(label);
                         label = new Label();
                         label.Text = string.Format("Iteration " + i);
                         Controls.Add(label);                    
                         i++;
                         oWebsite.Dispose();
                     }
                 }
            }

    Tuesday, April 26, 2011 12:06 AM

All replies

  • I wonder if someone replies to this ! :)

    Saturday, April 23, 2011 11:04 PM
  • For a Visual Web Part (basically a .Net user control)

    File, New Project, pick your language, SharePoint, 2010,Visual Web Part

    (not that a visual web part cannot be deployed as a Sandbox Solution)

     

    For a conventional web part:

    File, New Project, pick your language, SharePoint, 2010, Empty SharePoint Project and then once opened Project, Add New Item, Web Part.

    (or as a purist you could just create a .Net web part project, reference the SharePoint assemblies and then deploy the DLL by hand...  ;-)   )

     


    Mike Smith TechTrainingNotes.blogspot.com
    Saturday, April 23, 2011 11:56 PM
  • Hi,

    I have created visual Webpart in VS 2010. Here, my current job to access lists and libraries available on the sharepoint. I don't know where I have to write code for that ( since I found two c# files visualwebpart1.cs and VisualWebPart1UserControl.ascx.cs). I mean I have to Sharepoint objects to access data of the sharepoint server.

    Thanks.

    Sunday, April 24, 2011 9:47 AM
  • Hi, Can I use this statement to get all the lists available on the sharepoint

    SPList oListCur = SPContext.Current.List;

    Thanks.

    Sunday, April 24, 2011 10:33 AM
  • Hi,

    I wrote this code to access the number of sites and URL's are available in the Sharepoint.But it ididn't print anything eventhough it is a console application.

    using(SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName))
    {
      SPWebCollection collWebsites = oSiteCollection.AllWebs;
      Console.WriteLine("Count: {0}", collWebsites.Count);
    
      foreach (SPWeb oWebsite in collWebsites)
      {
        Console.WriteLine("Web site: {0}", oWebsite.Url);
        oWebsite.Dispose();
      }
    }
    Sunday, April 24, 2011 11:30 PM
  • If you're good with VS 2010 you don't need to rely on console applications - use breakpoints and the immediate window instead. Put a breakpoint after creating the SPSite and see whats going on. Mouse over the variables in the source code and you can begin to examine them. Put phrases inside the immediate window like oSite.AllWebs.Count and see what it says.
    Monday, April 25, 2011 12:37 AM
  • I put a statement Console.WriteLine(" You are about to start Site Collection...........") before SPSite statement. I deployed this webpart on sharepoint and added to a site.

    I can't see this message anywhere.

    Thanks.

    Monday, April 25, 2011 10:59 AM
  • Now, I have created a visual webpart and placed two server controls - RadioButton and Button. In the controls, I put a Response statement like Response.Write("You have clicked radiobutton")

    When I select radio button or click button, I am getting the message that i put but on the top of that webpart page ( i can see controls are in the page). I would like to see message in the webpart page.

    Thanks.

    Monday, April 25, 2011 11:48 AM
  • Hi,If you're developing a Web Part you should not drop a message by Console.WriteLine method but you could either show some text by a Response.Write method in your UserControl or HttpContext.Current.Response.Write method in your WebPart. Also you may either use Writer.Write method in 

    protected override void RenderContents(HtmlTextWriter Writer)
    
    method or just put a Literal control on the page and assign it a value.


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Monday, April 25, 2011 12:08 PM
  • Hi, I used Response.Write and HttpContext.Current.Response.Write but I got the same ! Please check out this image http://img94.imageshack.us/i/beforehh.jpg/

    I clicked a control in a webpart, I was expecting to this message in the same page ( like postback) but it is going another page.

    Thanks.


    Monday, April 25, 2011 7:38 PM
  • If this web part is deployed through VS2010 you should be able to debug it. Go to a line of code you know runs and press F9. you should see a dark red bar appear on that line of code. Then press F5 to run your web part inside the sharepoint web site (assuming you are on a machine that has both VS2010 and SharePoint on the same box). You should see VS2010 stop at your breakpoint. Does this work for you?

    I only say this because Response.Write is really a last resort. You pay big money for a powerful VS2010 debugger. Use it!

    Monday, April 25, 2011 7:55 PM
  • Thanks a lot for your replies !

    I am just wondering about the usage of debugging here. I didn't get the purpose of debugging here.

    Here is the code I have written. Please check out this image http://img94.imageshack.us/i/beforehh.jpg/

    namespace VisualWebPartProject1.VisualWebPart1
    {
        public partial class VisualWebPart1UserControl : UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
            {
                //Response.Write("You have clicked button1");

                HttpContext.Current.Response.Write("You are doing fantastic job");
            }

            protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
            {
                Response.Write("You have clicked button 2");
            }

            protected void Button1_Click1(object sender, EventArgs e)
            {
                int i = 0;
                Response.Write(" You are about to start the project...");
                using (SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName))
                {


                    SPWebCollection collWebsites = oSiteCollection.AllWebs;
                    Response.Write("Count: {0}" + collWebsites.Count);

                    foreach (SPWeb oWebsite in collWebsites)
                    {
                        Response.Write("Web site: {0}" + oWebsite.Url);
                        Response.Write("Iteration" + i);
                        i++;
                        oWebsite.Dispose();

                    }

                }

            }

           
        }
    }

    Monday, April 25, 2011 8:08 PM
  • So if you remove all the response.writes what is your problem? You are clicking button1 and not seeing the expected output, even though through debugging you see every line of code getting hit?
    Monday, April 25, 2011 8:20 PM
  • Hi, I did debugging. Here - when I click the control in the webpart, it is rendering a new page with a message ( that I put in that control). I would like that message in the same webpart.

    Thanks a lot :)

    Monday, April 25, 2011 10:20 PM
  • create a new visual web part, drag a button onto the design surface, double click the button, and add this code to the code behind. This will render text inside your web part. No other windows should pop up or anything like that. I don't know what i was looking at with your screenshot - why was there a big "stop editing" button at the center of the page? Was this web part placed on a normal web part page like home.aspx? anyway...

     

            protected void Button1_Click(object sender, EventArgs e)
            {
                 int i = 0;
                 var label = new Label();
                 Controls.Add(label);
                 label.Text = " You are about to start the project...";
                 using (SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName))
                 {
                     SPWebCollection collWebsites = oSiteCollection.AllWebs;
                     label = new Label();
                     label.Text = string.Format("Count: {0}",  collWebsites.Count);
                     Controls.Add(label);                
                     foreach (SPWeb oWebsite in collWebsites)
                     {
                         label = new Label();
                         label.Text = string.Format("Web site: {0}", oWebsite.Url);
                         Controls.Add(label);
                         label = new Label();
                         label.Text = string.Format("Iteration " + i);
                         Controls.Add(label);                    
                         i++;
                         oWebsite.Dispose();
                     }
                 }
            }

    Tuesday, April 26, 2011 12:06 AM