Vinay Chavadi's Blog on Application Development in SharePoint 2010/2013/2016 & SharePoint Online
Friday, June 18, 2010
Restricting Menu Items in ListView
Add the following javascript lines in the Edit Page of the list from sharepoint designer.
_spBodyOnLoadFunctionNames.push("hideListViewToolbarItems('Edit in Datasheet','export to Spreadsheet','create column','view rss feed','settings:create view')");
function hideListViewToolbarItems()
{
var menuItem;
var menuItemName;
var menuItemIndex=-1;
var menuItemNames=new Array("edit in datasheet",
"open with windows explorer",
"connect to outlook",'export to spreadsheet','view rss feed','alert me'
,"create column","settings:create view","list settings",
"document library settings","explorer view","all documents",
"all items","modify this view",
"view:create view","new document",
"new item","new folder","upload document",
"upload multiple documents");
var menuItems = new Array("EditInGridButton",
"OpenInExplorer","OfflineButton",
"ExportToSpreadsheet","ViewRSS",
"SubscribeButton","AddColumn",
"AddView","ListSettings","ListSettings",
"View1","DefaultView",
"DefaultView","ModifyView","CreateView",
"New0","New0",
"NewFolder","Upload","MultipleUpload");
var allMenuItems = document.getElementsByTagName('ie:menuitem');
for(var i = 0; i < hideListViewToolbarItems.arguments.length; i++ )
{
menuItemName= hideListViewToolbarItems.arguments[i].toLowerCase();
for (j=0; j < menuItemNames.length; j++)
{
if(menuItemNames[j]==menuItemName)
{
menuItemIndex = j;
break;
}
}
menuItem=menuItems[menuItemIndex];
for (var l = 0; l < allMenuItems.length; l++)
{
if(menuItemName.indexOf(":")!=-1)
{
menuItemName = menuItemName.split(":")[1];
}
if (allMenuItems[l].id.indexOf(menuItem)!=-1
&& allMenuItems[l].text.toLowerCase() == menuItemName)
{
// For FireFox Compatibility
var parentNodeOfMenuItem = allMenuItems[l].parentNode;
parentNodeOfMenuItem.removeChild(allMenuItems[l]);
break;
}
}
}
}
_spBodyOnLoadFunctionNames.push("hideListViewToolbarItems('Edit in Datasheet','export to Spreadsheet','create column','view rss feed','settings:create view')");
function hideListViewToolbarItems()
{
var menuItem;
var menuItemName;
var menuItemIndex=-1;
var menuItemNames=new Array("edit in datasheet",
"open with windows explorer",
"connect to outlook",'export to spreadsheet','view rss feed','alert me'
,"create column","settings:create view","list settings",
"document library settings","explorer view","all documents",
"all items","modify this view",
"view:create view","new document",
"new item","new folder","upload document",
"upload multiple documents");
var menuItems = new Array("EditInGridButton",
"OpenInExplorer","OfflineButton",
"ExportToSpreadsheet","ViewRSS",
"SubscribeButton","AddColumn",
"AddView","ListSettings","ListSettings",
"View1","DefaultView",
"DefaultView","ModifyView","CreateView",
"New0","New0",
"NewFolder","Upload","MultipleUpload");
var allMenuItems = document.getElementsByTagName('ie:menuitem');
for(var i = 0; i < hideListViewToolbarItems.arguments.length; i++ )
{
menuItemName= hideListViewToolbarItems.arguments[i].toLowerCase();
for (j=0; j < menuItemNames.length; j++)
{
if(menuItemNames[j]==menuItemName)
{
menuItemIndex = j;
break;
}
}
menuItem=menuItems[menuItemIndex];
for (var l = 0; l < allMenuItems.length; l++)
{
if(menuItemName.indexOf(":")!=-1)
{
menuItemName = menuItemName.split(":")[1];
}
if (allMenuItems[l].id.indexOf(menuItem)!=-1
&& allMenuItems[l].text.toLowerCase() == menuItemName)
{
// For FireFox Compatibility
var parentNodeOfMenuItem = allMenuItems[l].parentNode;
parentNodeOfMenuItem.removeChild(allMenuItems[l]);
break;
}
}
}
}
Connected WebParts in WSS 3.0 and MOSS 2007
ASP.NET 2.0 web part connection framework contains a set of predefined interfaces and transformers for these predefined Consumer and Provider Interfaces.
The following is an overview of the predefined interfaces that are available in the ASP.NET 2.0
web part connections framework (all interfaces are located in the System.Web.UI.WebControls.
WebParts namespace):
• IWebPartField interface: This interface is used when web parts want to exchange a single
value. This interface is comparable to cell interfaces in the SharePoint 2003 web part connection
framework. Classes supporting the web part field interface are very suitable for
implementing data enhancement scenarios.
• IWebPartRow interface: This interface is used when web parts want to exchange a row of
information. This interface is comparable to row interfaces in the SharePoint 2003 web part
connection framework. Classes supporting the web part row interface are very suitable for
implementing master/detail scenarios.
• IWebPartTable interface: This interface is used when web parts want to exchange an entire
list of data (a set of rows). This interface is comparable to list interfaces in the SharePoint
2003 web part connection framework. Classes supporting the web part table interface are
very suitable for implementing alternate-view scenarios.
• IWebPartParameters interface: This interface is used when web parts want to exchange a
list of parameters (or, if you will, a property bag containing various values). This interface
is comparable to the ParamsOut interfaces of the SharePoint 2003 web part connection
framework.
ProviderWebPart:
namespace TestWebPartConnection
{
[ToolboxData("<{0}:ProviderWebPart runat=server>")]
[Guid("59bf0dbb-e31a-4d6b-8295-76f4def28470")]
public class ProviderWebPart : WebPart, IWebPartField
{
TextBox InputBox;
Button SubmitButton;
[ConnectionProvider("Web part Connection Provider")]
public IWebPartField GetWPConnectFieldProvider()
{
return this;
}
public void GetFieldValue(FieldCallback callback)
{
callback.Invoke(InputBox.Text);
}
public PropertyDescriptor Schema
{
get
{
return TypeDescriptor.GetProperties(this)["Web part Connection Provider"];
}
}
protected override void Render(HtmlTextWriter output)
{
//Make sure that all necessary child controls are created
//EnsureChildControls();
CreateChildControls();
//Render the TextBox
InputBox.Enabled = true;
output.Write("Enter the text ");
InputBox.RenderControl(output);
//Create a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
//Render the button
SubmitButton.Enabled = true;
SubmitButton.RenderControl(output);
}
///
/// Creates all the user interface controls necessary for the web part
///
protected override void CreateChildControls()
{
//Create The Text Bob
InputBox = new TextBox();
InputBox.ID = "InputBox";
InputBox.Enabled = false;
//Add to the Control List
Controls.Add(InputBox);
//Create the button
SubmitButton = new Button();
SubmitButton.ID = "SubmitButton";
SubmitButton.Text = "Submit";
SubmitButton.Enabled = false;
//Add to the control list
Controls.Add(SubmitButton);
SubmitButton.Click += new EventHandler(SubmitButtonClick);
}
private void SubmitButtonClick(object sender, EventArgs e)
{
}
}
}
Consumer Webpart:
namespace TestWebPartConnection
{
[ToolboxData("<{0}:ConsumerWebPart runat=server>")]
[Guid("18789938-92d3-40ca-bd44-014644f7c884")]
public class ConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtBox;
string name = string.Empty;
public string Name
{
get { return name; }
set { name = value; }
}
public ConsumerWebPart()
{
}
[ConnectionConsumer("Web Part Consumer")]
public void GetWPConnectedProviderInterface(IWebPartField connectProvider)
{
FieldCallback callback = new FieldCallback(ReceiveField);
connectProvider.GetFieldValue(callback);
}
public void ReceiveField(object objField)
{
if (objField != null)
{
this.Name = (string)objField;
if (this.Name != "")
txtBox.Text = this.Name;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label = new Label();
label.Text = "Name:";
txtBox = new TextBox();
Controls.Add(txtBox);
// TODO: add custom rendering code here.
// this.Controls.Add(label);
}
}
}
The following is an overview of the predefined interfaces that are available in the ASP.NET 2.0
web part connections framework (all interfaces are located in the System.Web.UI.WebControls.
WebParts namespace):
• IWebPartField interface: This interface is used when web parts want to exchange a single
value. This interface is comparable to cell interfaces in the SharePoint 2003 web part connection
framework. Classes supporting the web part field interface are very suitable for
implementing data enhancement scenarios.
• IWebPartRow interface: This interface is used when web parts want to exchange a row of
information. This interface is comparable to row interfaces in the SharePoint 2003 web part
connection framework. Classes supporting the web part row interface are very suitable for
implementing master/detail scenarios.
• IWebPartTable interface: This interface is used when web parts want to exchange an entire
list of data (a set of rows). This interface is comparable to list interfaces in the SharePoint
2003 web part connection framework. Classes supporting the web part table interface are
very suitable for implementing alternate-view scenarios.
• IWebPartParameters interface: This interface is used when web parts want to exchange a
list of parameters (or, if you will, a property bag containing various values). This interface
is comparable to the ParamsOut interfaces of the SharePoint 2003 web part connection
framework.
ProviderWebPart:
namespace TestWebPartConnection
{
[ToolboxData("<{0}:ProviderWebPart runat=server>")]
[Guid("59bf0dbb-e31a-4d6b-8295-76f4def28470")]
public class ProviderWebPart : WebPart, IWebPartField
{
TextBox InputBox;
Button SubmitButton;
[ConnectionProvider("Web part Connection Provider")]
public IWebPartField GetWPConnectFieldProvider()
{
return this;
}
public void GetFieldValue(FieldCallback callback)
{
callback.Invoke(InputBox.Text);
}
public PropertyDescriptor Schema
{
get
{
return TypeDescriptor.GetProperties(this)["Web part Connection Provider"];
}
}
protected override void Render(HtmlTextWriter output)
{
//Make sure that all necessary child controls are created
//EnsureChildControls();
CreateChildControls();
//Render the TextBox
InputBox.Enabled = true;
output.Write("Enter the text ");
InputBox.RenderControl(output);
//Create a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
//Render the button
SubmitButton.Enabled = true;
SubmitButton.RenderControl(output);
}
///
/// Creates all the user interface controls necessary for the web part
///
protected override void CreateChildControls()
{
//Create The Text Bob
InputBox = new TextBox();
InputBox.ID = "InputBox";
InputBox.Enabled = false;
//Add to the Control List
Controls.Add(InputBox);
//Create the button
SubmitButton = new Button();
SubmitButton.ID = "SubmitButton";
SubmitButton.Text = "Submit";
SubmitButton.Enabled = false;
//Add to the control list
Controls.Add(SubmitButton);
SubmitButton.Click += new EventHandler(SubmitButtonClick);
}
private void SubmitButtonClick(object sender, EventArgs e)
{
}
}
}
Consumer Webpart:
namespace TestWebPartConnection
{
[ToolboxData("<{0}:ConsumerWebPart runat=server>")]
[Guid("18789938-92d3-40ca-bd44-014644f7c884")]
public class ConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtBox;
string name = string.Empty;
public string Name
{
get { return name; }
set { name = value; }
}
public ConsumerWebPart()
{
}
[ConnectionConsumer("Web Part Consumer")]
public void GetWPConnectedProviderInterface(IWebPartField connectProvider)
{
FieldCallback callback = new FieldCallback(ReceiveField);
connectProvider.GetFieldValue(callback);
}
public void ReceiveField(object objField)
{
if (objField != null)
{
this.Name = (string)objField;
if (this.Name != "")
txtBox.Text = this.Name;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label = new Label();
label.Text = "Name:";
txtBox = new TextBox();
Controls.Add(txtBox);
// TODO: add custom rendering code here.
// this.Controls.Add(label);
}
}
}
Friday, June 11, 2010
Using Ajax Update Progress in ASP.Net
The following code snippets illusrate using ajax UpdateProgress in ASP.Net Applications:
Defaut.aspx
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
Defaut.aspx
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
Delete listitems from Sharepoit List
We cannot use the following code, we may get the following error:
"Collection was modified; enumeration operation may not execute."
using (SPSite site = new SPSite("http://server")) {
using (SPWeb web = siteCollection.OpenWeb()) {
SPList list = web.Lists["MyList"];
foreach (SPListItem item in list.Items) {
item.Delete();
}
}
}
We also cannot use the following code:
for (int i = 0; i < list.Items.Count; i++) {
list.Items.Delete(i);
}
The correct code is as follows:
The correct method for deleting list items is to use a decrementing For loop.
for (int i = list.Items.Count - 1; i >= 0; i--) {
list.Items.Delete(i);
}
For details refer the following link.
"Collection was modified; enumeration operation may not execute."
using (SPSite site = new SPSite("http://server")) {
using (SPWeb web = siteCollection.OpenWeb()) {
SPList list = web.Lists["MyList"];
foreach (SPListItem item in list.Items) {
item.Delete();
}
}
}
We also cannot use the following code:
for (int i = 0; i < list.Items.Count; i++) {
list.Items.Delete(i);
}
The correct code is as follows:
The correct method for deleting list items is to use a decrementing For loop.
for (int i = list.Items.Count - 1; i >= 0; i--) {
list.Items.Delete(i);
}
For details refer the following link.
Programmatically Create a Sharepoint List (SPList)
The following code snippet can be used to create a list in sharepoint site. The following example I have illustrated only DocumentLibrary creation.
public static bool CreateSPList(SPWeb web, string listName, SPListTemplateType type)
{
bool create = false;
try
{
if (type == SPListTemplateType.DocumentLibrary)
{
web.Lists.Add(listName, listName, SPListTemplateType.DocumentLibrary);
return true;
}
}
catch (Exception ex)
{
return false;
}
return create;
}
public static bool CreateSPList(SPWeb web, string listName, SPListTemplateType type)
{
bool create = false;
try
{
if (type == SPListTemplateType.DocumentLibrary)
{
web.Lists.Add(listName, listName, SPListTemplateType.DocumentLibrary);
return true;
}
}
catch (Exception ex)
{
return false;
}
return create;
}
Programmatically Create or Get Folder in Sharepoint
To create or get a folder from Sharepoint Site the following code snippet can be used:
public static SPFolder CreateORGetFolder( SPWeb web, string listName, string folderUrl)
{
SPList targetList = web.Lists[listName];
if (string.IsNullOrEmpty(folderUrl))
return targetList.RootFolder;
SPFolder folder = targetList.ParentWeb.GetFolder(web.Url + "/" + targetList.RootFolder.Url + "/" + folderUrl);
if (!folder.Exists)
{
if (!targetList.EnableFolderCreation)
{
targetList.EnableFolderCreation = true;
targetList.Update();
} // We couldn't find the folder so create it
string[] folders = folderUrl.Trim('/').Split('/');
string folderPath = string.Empty;
for (int i = 0; i < folders.Length; i++)
{
folderPath += "/" + folders[i];
folder = targetList.ParentWeb.GetFolder(web.Url + "/" + targetList.RootFolder.Url + folderPath);
if (!folder.Exists)
{
SPListItem newFolder = targetList.Items.Add("" , SPFileSystemObjectType.Folder,folderUrl);
newFolder.Update();
folder = newFolder.Folder;
}
}
}
// Still no folder so error out
if (folder == null)
throw new SPException(string.Format("The folder '{0}' could not be found.", folderUrl));
return folder;
}
public static SPFolder CreateORGetFolder( SPWeb web, string listName, string folderUrl)
{
SPList targetList = web.Lists[listName];
if (string.IsNullOrEmpty(folderUrl))
return targetList.RootFolder;
SPFolder folder = targetList.ParentWeb.GetFolder(web.Url + "/" + targetList.RootFolder.Url + "/" + folderUrl);
if (!folder.Exists)
{
if (!targetList.EnableFolderCreation)
{
targetList.EnableFolderCreation = true;
targetList.Update();
} // We couldn't find the folder so create it
string[] folders = folderUrl.Trim('/').Split('/');
string folderPath = string.Empty;
for (int i = 0; i < folders.Length; i++)
{
folderPath += "/" + folders[i];
folder = targetList.ParentWeb.GetFolder(web.Url + "/" + targetList.RootFolder.Url + folderPath);
if (!folder.Exists)
{
SPListItem newFolder = targetList.Items.Add("" , SPFileSystemObjectType.Folder,folderUrl);
newFolder.Update();
folder = newFolder.Folder;
}
}
}
// Still no folder so error out
if (folder == null)
throw new SPException(string.Format("The folder '{0}' could not be found.", folderUrl));
return folder;
}
Programmatically Check Folder Exists in Sharepoint
The following code can be used to check the folder exists in Sharepoint Site:
public static SPFolder CheckFolderExists(SPList targetList, string folderUrl)
{
SPFolder folder = null;
if (string.IsNullOrEmpty(folderUrl))
return targetList.RootFolder;
SPFolderCollection col = targetList.ParentWeb.Folders;
foreach (SPFolder item in col)
{
foreach (SPFolder fol1 in item.SubFolders)
{
foreach (SPFolder fol2 in fol1.SubFolders)
{
if (fol2.Name.ToLower() == folderUrl.ToLower())
{
folder = fol2;
return folder;
}
}
}
}
return folder;
}
public static SPFolder CheckFolderExists(SPList targetList, string folderUrl)
{
SPFolder folder = null;
if (string.IsNullOrEmpty(folderUrl))
return targetList.RootFolder;
SPFolderCollection col = targetList.ParentWeb.Folders;
foreach (SPFolder item in col)
{
foreach (SPFolder fol1 in item.SubFolders)
{
foreach (SPFolder fol2 in fol1.SubFolders)
{
if (fol2.Name.ToLower() == folderUrl.ToLower())
{
folder = fol2;
return folder;
}
}
}
}
return folder;
}
Subscribe to:
Posts (Atom)
-
There are 3 SPJobLockType available: 1. SPJobLockType.None -- if you set it none, the instance will run in all the available s...
-
Overview: We define disaster recovery as the ability to recover from a situation in which a data center that hosts SharePoint Server beco...