Tuesday 24 April 2012

How to fetch sharepoint list data into a grid - Server Object Model




Here in this example in my sharepoint site i have created  a  list called "Employee Details"
with two columns "Name" and "EmpID".


Now in my sharepoint visual webpart project ,i have placed a datagrid and renamed it to  "grdDisplay".


Then i used the following in the code behind page to fetch data from the list into the datagrid.


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.IO;

namespace fetchlistdata
{
    public partial class fetchlistdataUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridData();
        }

        protected void gridData()
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Employee Details"];
            SPQuery query = new SPQuery();
           
            SPListItemCollection items = list.GetItems(query);

            DataTable tbl = new DataTable();
            tbl.Columns.Add("Name");
            tbl.Columns.Add("EmpID");

            DataRow tr;
            foreach (SPListItem item in items)
            {
                tr = tbl.Rows.Add();
                tr["Name"] = Convert.ToString(item["Name]);
                tr["EmpID"] = Convert.ToString(item["EmpID"]);

            }

            grdDisplay.DataSource = tbl;
            grdDisplay.DataBind();

                   
        }
}
}

Friday 20 April 2012

How to restore a top level site to a subsite using sharepoint powershell

Restore toplevel site to subsite
--------------------------------------

first export  the top level site http://server1:1111/  using  sharepoint powershell


Export :       stsadm -o export -url <fullurl which u want to export> -filename <location:filename.dat>


Now create a subsite  in another sharepoint server


Import :         stsadm -o import -url <destination url> -filename <location where u stored.dat file>


 Replace  Destination  Url with subsite url   example -- http://server2:1234/subsite/

Thursday 19 April 2012

How To get all the users From sharepoint groups in client object model

In the below example i have retrieved all the sharepoint users and populated it in a combobox.

Silverlight  Main Page  Program

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

namespace users
{

public partial class MainPage : UserControl
{
string user;
GroupCollection Groupcoll;
ClientContext site;
public  MainPage()
{


InitializeComponent();
site = new ClientContext(ApplicationContext.Current.Url);
site.Load(site.Web);
Groupcoll= site.Web.SiteGroups;
site.Load(Groupcoll, groups => groups.Include(group => group.Users));
site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

}
private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
// This is not called on the UI thread.
Dispatcher.BeginInvoke(datacon);
}

private void datacon()
{
foreach (Group Grp in Groupcoll)
{
UserCollection collUser = Grp.Users;
foreach (User Usr in collUser)
{
user = Convert.ToString(Usr.Title);
combobox1.Items.Add(user);
}
}
}



OUTPUT
}
}

Tuesday 17 April 2012

Ajax ToolKit Configuration For Sharepoint


Before  you   use  Ajax Toolkit  Controls  in  your  visual  web part, you need  to  configure
sharepoint.

STEP 1
copy  the  ajaxtoolkit.dll  to  C:\Windows\assembly\  Folder .

STEP 2
Now  inside the  C:\Windows\assembly\   select  ajaxtoolkit  assembly  right click then properties

                                                                                                                                                                    


Note the public key token ,culture,version no.

STEP 3
Now  edit  the  web.config  file  in C:\inetpub\wwwroot\wss\VirtualDirectories\1111\web.config  --replace 1111 with your site port no
















STEP 4
Now add the following tag  inside the web.config file.

<assemblies>
<add assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" />
</assemblies>  

Now you can add ajax toolkit  controls to your  webpart.

Monday 16 April 2012

How to get values From Multiple Values LookUp Field in Silverlight Client Object Model


In the below example  i  have  fetched  values from a multi values Look Up Field  and  populated a  treeview.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace multilookupvalue
{
public partial class MainPage : UserControl
{
ClientContext cc;
string name;
int recordno;
public MainPage()
{

InitializeComponent();
cc = new ClientContext(ApplicationContext.Current.Url);
cc.Load(cc.Web);
listobj = cc.Web.Lists.GetByTitle("your list name");
List s = cc.Web.SiteUserInfoList;
cc.Load(listobj);
CamlQuery cml = new Microsoft.SharePoint.Client.CamlQuery();
view = "<view/>";
cml.ViewXml = view;
_lsititemcoll = listobj.GetItems(cml);
cc.Load(_listitemcoll);
cc.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

}
private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{

// This is not called on the UI thread.
Dispatcher.BeginInvoke(datacon);

}
private void datacon()
{
recordno = 1;
treeview tv1 = new treview();
foreach(ListItem li in _listitemcoll)
{

FieldLookupValue[]  fvalue  =   li["lookup column  name"]  as FieldLookupValue[];
TreeViewItem item = new TreeViewItem();
item.Header = "Record" + recordno;
for(int i=0;i<fvalue.Count();i++)
{

TreeViewItem subitem = new TreeViewItem();
subitem.Header =fvalue[i].LookUpValue;
item.Items.Add(subitem);

}
tv1.Children.Add(item);
LayoutRoot.Children.Add(tv1);
}

}
}

How to get Sharepoint List User Field Value in Silverlight Client Object Model

you can retrieve  the  user field  value  in  client  object  model  as  follows


In the below example,i have retrieved  all the values from a  user column and populated  a  list box.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace lookupvalue
{
public partial class MainPage : UserControl
{
ClientContext cc;

public MainPage()
{

InitializeComponent();
cc = new ClientContext(ApplicationContext.Current.Url);
cc.Load(cc.Web);
listobj = cc.Web.Lists.GetByTitle("your list name");
List s = cc.Web.SiteUserInfoList;
cc.Load(listobj);
CamlQuery cml = new Microsoft.SharePoint.Client.CamlQuery();
view = "<view/>";
cml.ViewXml = view;
_lsititemcoll = listobj.GetItems(cml);
cc.Load(_listitemcoll);
cc.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

}
private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{

// This is not called on the UI thread.
Dispatcher.BeginInvoke(datacon);

}
private void datacon()
{
foreach(ListItem li in _listitemcoll)
{

FieldUserValue  fvalue  =   li["User Field column  name"]  as  FieldUserValue;
listBox1.Items.Add(fvalue.LookupValue);

}
}
}

How To Access Silverlight function using javascipt - sharepoint

Create  a   silverlight  application  as  follows

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;
namespace sljsinterop
{
[ScriptableType]
public partial classMainPage : UserControl
{
public MainPage()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("Page", this);
}
[ScriptableMember]
publicvoid fn(string s)
{
MessageBox.Show(s);
}
}
}
}

The [ScriptableType] attribute  over the class and the [ScriptableMember] over the function provides access for  javascript to invoke  the function.

Now  Build  the  solution and keep  the  .Xap  file  seperately.

First  Create  New Project in  Visual Studio



Then  in  the Project  templates  select   Sharepoint  2010  --->  Empty Project



Select  the  site that  you  want   to deploy  the  web  part  to.



Then  in the  solution explorer  right  click on the project file  and  click add --->New Item

                     



Then in the item template  choose  Sharepoint 2010  ---->   visual webpart template



Now  open  the   design page  of  the  Visual  Webpart




Now  paste   the  xap   file  that  we   created   into   the project  and  include it in the visual  webpart  using  the <Object>  tag.

<objectid="sl1"data="data:application/x-silverlight-2,"type="application/x-silverlight-2"width="600"height ="600">
 <param name="source"value="sljsinterop.xap"/>
 <paramname="onError"value="onSilverlightError"/>
 <paramname="background"value="white"/>
 <paramname="minRuntimeVersion"value="4.0.50826.0"/>
 <paramname="autoUpgrade"value="true"/>
</object>



I have given ID for the object tag as "sl1".Now in Javascript  we are going to create an object of sl1 and use it to call function from silverlight.

The  below  javascript  code  can be present inside the visualwebpart  or  in the page where the webpart is used,you can also use this inside a content editor.

<script language="javascript" type="text/javascript">
function Button1_onclick() {
var sl = document.getElementById("sl1");
sl.focus();
sl.Content.Page.fn("invoked from javascript");
}
 </script>