How to get Site, Web, and List name from a list url
Some time we have url of list and need to find its
web, list name ...
Basically, we need to split string and get web name and list.
We have SharPoint to do this in easy way and
also dynamic... :)
Using siteCollection As New
SPSite(listurlfromitems) Dim
Site As SPSite = siteCollection Dim
myWeb As SPWeb = siteCollection.OpenWeb() Dim
_list As SPList = myWeb.GetList(listurlfromitems) End Using
Here listurlfromitems is a string which holds
URL of list after that we can find site, web, and list in
Site, myWeb, _list variable
Public Function MoveListItemsSiteToSite(sourceSiteURL As String, sourceList As String, destinationSiteURL As String, destinationList As String, retainMeta As Boolean) As Boolean
Using sourceSite As New SPSite(sourceSiteURL)
Using sourceWeb As SPWeb = sourceSite.OpenWeb()
sourceWeb.AllowUnsafeUpdates = True
' Get your source library
Dim source As SPList = sourceWeb.Lists(sourceList)
' Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
Dim items As SPListItemCollection = source.Items
Dim fileCount As Integer = 0
Using destSite = New SPSite(destinationSiteURL)
Using destinationWeb = destSite.OpenWeb()
destinationWeb.AllowUnsafeUpdates = True
' get destination library
Dim destination As SPList = destinationWeb.Lists(destinationList)
' Get the root folder of the destination we'll use this to add the files
Dim destinationFolder As SPFolder = destinationWeb.GetFolder(destination.RootFolder.Url)
' Now to move the files and the metadata
For Each item As SPListItem In items
'Get the file associated with the item
Dim file As SPFile = item.File
' Create a new file in the destination library with the same properties
Dim newFile As SPFile = destinationFolder.Files.Add(destinationFolder.Url + "/" + file.Name, file.OpenBinary(), file.Properties, True)
If retainMeta Then
Dim newItem As SPListItem = newFile.Item
WriteFileMetaDataFiletoFile(item, newItem)
End If
file.Delete()
fileCount += 1
Next
destinationWeb.AllowUnsafeUpdates = False
End Using
End Using
sourceWeb.AllowUnsafeUpdates = False
Return True
End Using
End Using
End Function
Public Shared Sub WriteFileMetaDataFiletoFile(sourceItem As SPListItem, destinationItem As SPListItem)
destinationItem("Editor") = sourceItem("Editor")
destinationItem("Modified") = sourceItem("Modified")
destinationItem("Modified By") = sourceItem("Modified By")
destinationItem("Author") = sourceItem("Author")
destinationItem("Created") = sourceItem("Created")
destinationItem("Created By") = sourceItem("Created By")
destinationItem.UpdateOverwriteVersion()
End Sub
Today i m trying to share with you one interesting things... like how can we find "who is responsible for postback ?"
very easily w to catch target controls who fired postback event.
Take a look of this code.
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If Page.Request.Params("__EVENTTARGET") = btnClikOk.UniqueID Then
txtResult.Text = "Ok Button has clicked"
End If
If Page.Request.Params("__EVENTTARGET") = Button1.UniqueID Then
txtResult.Text = "Cancel button has clicked"
End If
End Sub
Page.Request.Params("__EVENTTARGET") this one will return target control ID and by condition checking you can catch Control.
Modal dialog play very important role to improve the user experience by reducing the number of postbacks. So, SharePoint 2010 comes up with in-build API to show modal dialog to improve the user experience. Here, I will explain you How to integrate SharePoint 2010 modal dialog with the application and some real time problems and their solutions. Before I go on and provide you with the details, Let us see some of the features that this new Modal Dialog provides.
Functionality provided by Modal Dialog: - - Display HTML content in Modal Dialog - Display external url (e.g. http://www.google.com) or any application page of SharePoint in the form of an iframe - Show/Hide Close button - Show/Hide Maximize button
Mentioned below are the steps to integrate a modal dialog in the SharePoint 2010:
The JavaScript files for the ECMAScript Object Model (SP.js, SP.Core.js, SP.Ribbon.js, and SP.Runtime.js ) are installed in the %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS directory. We need to add these JavaScript files in the page.
To open a dialog we need to use the 'SP.UI.ModalDialog.showModalDialog' method from the ECMAScript Client Object model and we can pass following parameters as per requirement:
- width: Set the width of the modal dialog
- height: Set the height of the modal dialog
- html: the ID HTML control or HTML content to be displayed in modal dialog
- url: Page url or relative path
- dialogReturnValueCallback: In case we want some code to run after the dialog is closed, set JavaScript method name
- allowMaximize: Set to true or false to show hide this option.
- showClose: Set to true or false to show or hide the close button
Examples: a. Sample code to show HTML content in the SharePoint 2010 modal dialog:
// Call openDialog method on button click or on page load
function openDialog() {
var options = {
html: divModalDialogContent, // ID of the HTML tag
// or HTML content to be displayed in modal dialog
width: 600,
height: 300,
title: "My First Modal Dialog",
dialogReturnValueCallback: dialogCallbackMethod, // custom callback function
allowMaximize: true,
showClose: true
};
SP.UI.ModalDialog.showModalDialog(options);
}
//Results displayed if 'OK' or 'Cancel' button is clicked if the html content has 'OK' and 'Cancel' buttons
function onDialogClose(dialogResult, returnValue) {
if (dialogResult == SP.UI.DialogResult.OK) {
alert('Ok!');
}
if (dialogResult == SP.UI.DialogResult.cancel) {
alert('Cancel');
}
}
// Custom callback function after the dialog is closed
function dialogCallbackMethod() {
alert('Callback method of modal dialog!');
}
</script>
b. Sample code to open a web page or application page in the modal dialog:
JavaScript code
<scripttype="text/javascript">
function openDialog() {
var options = {
url: "<Page Url>",
width: 600,
height: 300,
title: "My First Modal Dialog",
};
SP.UI.ModalDialog.showModalDialog(options);
}
</script>
Mentioned below are real time scenarios, you may encounter while using out of box modal dialog of SharePoint 2010 :- 1. Scenario: If page is too long and with the movement of vertical scrollbar, modal popup also move with the scrollbar. Ideally position of modal dialog should be fix.
Solution: Put mentioned below CSS class in you page:
.ms-dlgContent
{
position: fixed!important;
}
2. Scenario: Display modal popup on page load depending. Sometimes the page don't show modal dialog and a JavaScript error message comes (error: showModalDialog does not exist in SP.UI.ModalDialog).
Solution: Use following code- ExecuteOrDelayUntilScriptLoaded(<showModelMethodName>, "sp.js"); This method be make sure that js method showModalDialog is not called till sp.js is fully loaded.
3. Scenario: Sometime we need to close popup from server side and also parent window refresh is required after saving modal dialog data in the SharePoint.
Solution: Mentioned below is the sample code to implement above requirement-
a. Check current page is popup page or not
//Custom list or library form New, Edit or Display if(SPContext.Current.IsPopUI)
{
// Code
}
OR
//Layout pages you can ensure byquery string
if(Request.QueryString["IsDlg"]=="1")
{
//code
}
b. To close popup use mentioned below JavaScript code:
<scripttype="text/javascript">
//Close popup on cancel button click
function CloseForm() {
window.frameElement.cancelPopUp();
returnfalse;
}
//Commit/Refresh parent window on save button click
function SaveForm() {
window.frameElement.commitPopup();
returnfalse;
}
</script>
c. use following server side code to close or save modal dialog:
//Put following code in the button click event, if update panel is not present in the page
4. Scenario: After closing modal dialog, all controls disappear from the page. Solution: If you assign HTML element id like we have done in our first example, you might encounter this issue, we should set copy of modal data instead of assigning ID. See sample code below:
<scripttype="text/javascript">
//Pass copy of HTML content instead of content control ID
function openDialog() {
var cloneModalContent = document.createElement('div');
Find the url of the attachments
-------------------------------------------------
function getattachment(id)
{
var lists = new SPAPI_Lists('http://yoursite/Announcements');
var items = lists.getAttachmentCollection("Announcements",id);
if (items.status == 200){
var rows = items.responseXML.getElementsByTagName('Attachment');
if (rows.length>0){
var str="";
for (var i=0; i<rows.length; i++){
alert($(rows[i]).text());
}
}
else
{
return "/Announcements/Lists/Announcements/DispForm.aspx?ID="+id;
}
}
}
//Here my site is 'http://yoursite/Announcements' and my list name is "Announcements" and id=ID of the item
know wheather the item has any attachment or not
-------------------------------------------------------------------------
function list_itms(itemid)
{
var lists = new SPAPI_Lists('http://yoursite/Announcements');
var items = lists.getListItems('Announcements');
if (items.status == 200)
{
var rows = items.responseXML.getElementsByTagName('z:row'); // Get only list rows
for (var i=0; i<rows.length; i++)
{
if (itemid == ows_ID){
var newid =rows[i].getAttribute('ows_Attachments');
alert(newid);
}
}
}
}
//The above code is to know wheather the item has any attachment or not
//Here my site is 'http://yoursite/Announcements' and my list name is "Announcements" and id=ID of the item