Wednesday, June 8, 2011

System.ServiceModel.ProtocolException was unhandled by user code Message=The remote server returned an unexpected response: (405) Method Not Allowed.

Go to control panel : Programs and Feature : Turn windows features on/off [found on the left side navigation bar] :Microsoft .Net Framework 3.0 : Turn on WCF HTTP Activation Checkbox


This resolves the following exception.
System.ServiceModel.ProtocolException was unhandled by user code Message=The remote server returned an unexpected response: (405) Method Not Allowed.

Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at

Wednesday, March 9, 2011

Windows PowerShell and SharePoint Commands

go to Windows Powershell located at

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
you can also reach here from Accessories>WindowsPowershell

Type following command
add-pssnapin microsoft.sharepoint.powershell

you can verify by typing following command
get-command -noun sp*

this should return you thousands of commands available.

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);
}

}
}

}