Monday, February 28, 2011

WebPart to Upload to sharePoint Document Lib using SharePoint Object Model

//Following code using SharePoint Object Model and Fileupload class.
//It uploads a docuent to 'Shared Documents' document library.


using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace DocLibActivities.DocLibActivities
{
[ToolboxItemAttribute(false)]
public class DocLibActivities : WebPart
{

FileUpload oFileUploader;
protected override void CreateChildControls()
{
oFileUploader = new FileUpload();
this.Controls.Add(oFileUploader);



}
void btnUpload_Click(object sender, EventArgs e)
{

string strFilename = oFileUploader.FileName;
using (SPWeb objThisWeb = SPContext.Current.Web)
{
SPFolder objTargetFolder = objThisWeb.Lists["Shared Documents"].RootFolder;
foreach (SPFile objFile in objTargetFolder.Files)
{
if (Context.Request.InputStream.Length != null)
{

System.IO.Stream oIOStream = Context.Request.InputStream;
byte[] fbytes = new byte[oIOStream.Length];
oIOStream.Read(fbytes, 0, (int)oIOStream.Length);
oIOStream.Close();
SPFile oSPNewFile = objTargetFolder.Files.Add(strFilename, fbytes, true);
oSPNewFile.CheckOut();

}
this.Page.Response.Redirect(objThisWeb.Url + "/" + objTargetFolder.Url);
}
}

}


}
}

Iterating through SharePoint Document library and get File info

SPWeb oSpWeb = SPContext.Current.Web;
SPFolder oSPF = oSpWeb.Lists["Shared Documents"].RootFolder;

foreach (SPFile oSPfile in oSPF.Files)
{
if (oSPfile.CheckOutType == SPFile.SPCheckOutType.None)
{
LiteralControl ltc= new LiteralControl(string.Format("
File {0} : {1} : {2}", oSPfile.Name,"Not Checked Out", oSPfile.CheckInComment));
this.Controls.Add(ltc);

}
else
{
LiteralControl ltc= new LiteralControl(string.Format("
File {0} : {1} : {2}", oSPfile.Name,"Checked Out", oSPfile.CheckInComment));
this.Controls.Add(ltc);
}

}

Sunday, February 27, 2011

SharePoint 2010 Visual Webpart and List and SPGridView Population

Sharepoint list name: VSProducts
This has three fields ID,Manufacturer, Name
Following grid view get populated using the following code.

.ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RenderingData.ascx.cs" Inherits="RenderingData.VisualWebPart1.VisualWebPart1UserControl" %>



code behind


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;

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

if (!Page.IsPostBack)
{
populateSPGrid();
}


}
protected void populateSPGrid()
{
using (SPSite oSPSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{

SPList oList = oSPWeb.Lists["VSProducts"];
SPListItemCollection oListCollection = oList.Items;
DataTable table = new DataTable();

table.Columns.Add("ID", typeof(string));
table.Columns.Add("Manufacturer", typeof(string));
table.Columns.Add("Name", typeof(string));

// Create rows for each splistitem

DataRow row;

foreach (SPListItem oSPListItem in oListCollection)
{

row = table.Rows.Add();

row["ID"] = oSPListItem["ID"].ToString();

row["Manufacturer"] = oSPListItem["Manufacturer"].ToString();

row["Name"] = oSPListItem["Name"].ToString();

}

// create the bound fields

SPBoundField boundField;

boundField = new SPBoundField();

boundField.HeaderText = "ID";

boundField.DataField = "ID";

boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

boundField.ItemStyle.Wrap = false;

SPGView.Columns.Add(boundField);





boundField = new SPBoundField();

boundField.HeaderText = "Manufacturer";

boundField.DataField = "Manufacturer";

SPGView.Columns.Add(boundField);



boundField = new SPBoundField();

boundField.HeaderText = "Name";

boundField.DataField = "Name";

SPGView.AutoGenerateColumns = false;

SPGView.Columns.Add(boundField);





SPGView.DataSource = table.DefaultView;

SPGView.DataBind();

}

}

}

}
}

Thursday, February 17, 2011

Iterating through SPweb, SPlist and populating Treeview

Following code makes a tree view for a site (web) and its child sites, their lists.
It also displays the number of items each of that list has.

It’s a small Administration utility and very helpful for understanding how to iterate through spsite, spweb and lists.

I used Visual studio 2010. Its great tool to work with SharePoint 2010.
This really a great relied in terms of developing a webpart, packaging and deploying.

I have a user control there I added a tree view, name 'tviewSiteStructure'.

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

namespace SiteStructure.SiteStructureWebPart
{
public partial class SiteStructureWebPartUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

using (SPSite mySite = new SPSite("http://MyMOSS2010/CP"))
{
using (SPWeb myWeb = mySite.OpenWeb())
{

TreeNode mynode = new TreeNode(myWeb.Title, null, null, myWeb.Url, "_self");
TreeNode parentNode = mynode;
tviewSiteStructure.Nodes.Add(mynode);


foreach (SPList myList in myWeb.Lists)
{
string listLink = "" + myList.Title + "(" + myList.Items.Count.ToString() + ")
";

mynode = new TreeNode(listLink, null, null, myList.DefaultViewUrl, "_self");

parentNode.ChildNodes.Add(mynode);

}

foreach (SPWeb childweb in myWeb.Webs)
{
Iterateotherwebs(childweb, parentNode);
}
tviewSiteStructure.CollapseAll();
}
}

}
void Iterateotherwebs(SPWeb web, TreeNode ParentNode)
{
TreeNode mynode = new TreeNode(web.Title, null, null, web.Url, "_self");
TreeNode parentNode = mynode;
tviewSiteStructure.Nodes.Add(mynode);

foreach (SPList myList in web.Lists)
{
string listLink = "" + myList.Title + "("+myList.Items.Count.ToString()+")
";

mynode = new TreeNode(listLink, null, null, myList.DefaultViewUrl, "_self");
parentNode.ChildNodes.Add(mynode);

}

foreach (SPWeb childweb in web.Webs)
{
Iterateotherwebs(childweb, parentNode);
}

}
}

}