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

}
}

}

Wednesday, January 19, 2011

Error while accessing ListData.svc : ADO.NET Data Services

Could not load type 'System.Data.Services.Providers.IDataServiceUpdateProvider' from assembly 'System.Data.Services, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

#1
As error suggests, please ensure that you have System.Data.Services entry in the Global Assembly Cache (GAC)

#2 download and install
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a71060eb-454e-4475-81a6-e9552b1034fc&displaylang=en



#3 downloand and install the ADO.NET Data Services Update for .NET 3.5 SP1.

http://www.microsoft.com/downloads/en/details.aspx?familyid=4B710B89-8576-46CF-A4BF-331A9306D555&displaylang=en


#4 Do not forget to reset the IIS.

Wednesday, January 12, 2011

error occurred in deployment step 'Retract Solution': The language-neutral solution package was not found

#1
Run Powershell

#2
Ensure you have Sharepoint Powershell commands enabled
For that run following command

Add-PSSnapin Microsoft.SharePoint.Powershell

#3
run following command (Replace mysolution.wsp with your solution name)
(get-spsolution mysolution.wsp).Delete()

this helped to me.
If still not
then open your solution folder and delete files from bin,pkg and obj folders and do the above mentioned again.

debugging Sharepoint App using Visual Studio 2010 : No symbols have been loaded

Right click on solution --> Click on Properties
Click on 'Multiple startup projects' radio button
select the project you need to debug
Select Start from the action drop down for that project.

Sunday, January 9, 2011

SharePoint 2010 build error Warning 1 The Reference...

Warning 1 The referenced assembly “Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL” could not be resolved because it has a dependency on “System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project.


You may need to do the following to resolve the issue

SharePoint 2010 is built on top of .Net Framework 3.5. So make sure you have it as your targeted framework in VS. Go to project properties and change the target framework to “.Net Framework 3.5″.