Thursday 23 May 2013

WebPart Display the Name Of All Users who Visted Your SharePoint Site Using Visual Studio 2010

WebPart Display the Name Of All Users who Visted Your SharePoint Site Using Visual Studio 2010

Create a webpart using visualstudio 2010. For that do the follwing:


1.InVisualStudio 2010 Open File->Project.
2.Select SharePoint Template and select VisualWebPart.

3.Specify Your SharePoint Site where you wanted to create webpart and click Finish.
 
4.On Your Project place a listbox and a button and on Button Click event write the following code.



            list1.Items.Clear();
            SPWeb web = SPControl.GetContextWeb(Context);
            foreach (SPUser usr in web.AllUsers)
            {
                list1.Items.Add(usr.Name);
             }

NB : include namespace Microsoft.SharePoint; Microsoft.SharePoint.WebControls for getting SPWeb and SPControl;


5.Build and Deploy Solution.
6.The Webpart that created will displayed on Custom Category.Add the WebPart .




7.On Button Click the webpart will dispalay the Visited Users name


using (SPSite site = new SPSite("http://yourServer"))
{
         SPWeb web = site.OpenWeb();
         site.AllowUnsafeUpdates = true;
         web.AllowUnsafeUpdates = true;    
         SPListCollection coll = web.Lists;
           
         Guid gd = coll.Add("ListTitle", "ListDescription", SPListTemplateType.CustomGrid);
         coll[gd].Fields.Add("FirstName", SPFieldType.Text, true);
         coll[gd].Fields.Add("LastName", SPFieldType.Text, true);
         coll[gd].Fields.Add("Married", SPFieldType.Boolean, true);
         coll[gd].Update();
  
         string defaultquery = coll[gd].Views[0].Query;
         SPViewCollection viewcoll = coll[gd].Views;
         Guid anothergd = coll[gd].Views[0].ID;
         viewcoll.Delete(anothergd);
  
         System.Collections.Specialized.StringCollection viewfields = new System.Collections.Specialized.StringCollection();
         viewfields.Add("Title");
         viewfields.Add("FirstName");
         viewfields.Add("LastName");
         viewfields.Add("Married");
  
        coll[gd].Views.Add("View name", viewfields, defaultquery, 100, true,true);
        coll[gd].Update();
 }
SPList list = web.Lists["ListTitle"];
list.OnQuickLaunch = true;
list.Update();

No comments:

Post a Comment