none
get address from another site collection RRS feed

  • Question

  • i have two site collections http://test.local and http://test.local/sekt3/2020 i need spsite site for http://test.local in http://test.local/sekt3/2020

    how get 

    using SPSite site = new SPSite( over variable get http://test.local)

    {

    using SPWeb web = site.OpenWeb()

    {

    code for site collection http://test.local/sektr3/2020

    }

    }

    Wednesday, July 31, 2019 1:01 PM

Answers

  • Hi,

    Please try to use the address "http://localhost".

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, August 1, 2019 8:31 AM
    Moderator

All replies

  • Hi gogi100,

    you can use Content and Structure option to move the content from one site to another. below article for your reference:

    https://sharepointmaven.com/how-to-move-sites-in-sharepoint/

    Thanks & Regards,


    sharath aluri

    Wednesday, July 31, 2019 1:16 PM
  • no, i need this requirement because of my code. can i use some trim or split function?
    • Edited by gogi1000 Wednesday, July 31, 2019 1:23 PM edit
    Wednesday, July 31, 2019 1:20 PM
  • my code is next

    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Administration;
    using System.Collections;
    using System.Data;
    using Microsoft.SharePoint.Utilities;
    using System.Web;
    
    using System.Web.UI.WebControls;
    using System.Web.UI;
    
    using Microsoft.SharePoint.Client;
    
    
    
    namespace Obrasci.Layouts.Obrasci
    {
       
    
        public partial class TemplateSelector : LayoutsPageBase
           {
    
                    // datasource for the Template ListView
                 public readonly DataTable _dataTable = new DataTable();
                   
                    protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
    
                        SetTemplateLibraryFolders();
                    }
                  
                          _dataTable.Columns.Add("ImageUrl", typeof(string));
                         _dataTable.Columns.Add("Title", typeof(string));
                        _dataTable.Columns.Add("Url", typeof(string));
                       var selectedNode = tvFolders.SelectedNode == null ? tvFolders.Nodes[0].Value : tvFolders.SelectedNode.Value;
                     
                     GetTemplatesInSelectedFolder(selectedNode);
                    // set the templates as the datasource for the template list view
                      lvTemplates.DataSource = _dataTable;
                      lvTemplates.DataBind();
                 }
    
    
                    private void SetTemplateLibraryFolders()
                    {
                        SPSite currentsite = new SPSite("http://test-net.dri.local");
                        using (SPWeb web = currentsite.OpenWeb())
                        {
                            tvFolders.EnableClientScript = true;
                            tvFolders.Target = "_self";
                            tvFolders.ShowLines = true;
                            tvFolders.ExpandDepth = 1;
                            SPList list = web.Lists["Обрасци за ревизију"];
    
                            TreeNode rootnode = new System.Web.UI.WebControls.TreeNode(list.Title);
                            rootnode.SelectAction = TreeNodeSelectAction.SelectExpand;
                            rootnode.ImageUrl = currentsite.RootWeb.Url + "/_layouts/images/FOLDER16.GIF";
                            TraverseFolder(list.RootFolder, rootnode);
                            tvFolders.Nodes.Add(rootnode);
                          
                        //    placeHolder.Controls.Add(tvFolders);
                        }
                    }
    
    private void TraverseFolder(SPFolder sPFolder,TreeNode rootnode)
    {
     	 try
                        {
                            // add the files contained in this folder   
                            foreach (SPFile file in sPFolder.Files)
                            {
                                // create a new node and add to the tree   
                                TreeNode childNode = new System.Web.UI.WebControls.TreeNode(file.Name + " (" + file.TimeLastModified.ToShortDateString() + ")", "", "~/_layouts/images/" + file.IconUrl, file.ServerRelativeUrl.ToString(), "");
                                rootnode.ChildNodes.Add(childNode);
                            }
                            // test if we have child folders   
                            bool blnRecurseFolders = sPFolder.SubFolders.Count > 0 ? true : false;
                            SPSite currentsite = new SPSite("http://test-net.dri.local");
                            // if we have sub folders then loop through them   
                            if (blnRecurseFolders)
                            {
                                // loop through the child folders   
                                foreach (SPFolder childFolder in sPFolder.SubFolders)
                                {
                                    if (childFolder.Name.ToString() != "Forms")
                                    {
                                        // create a new node and loop through the files   
                                        TreeNode childNode = new TreeNode();
                                        childNode.Text = childFolder.Name;
                                        childNode.Value = childFolder.ServerRelativeUrl.ToString();
    
                                        childNode.ImageUrl = currentsite.RootWeb.Url + "/_layouts/images/FOLDER16.GIF";
                                        //SPUtility.ConcatUrls("/_layouts/images/", SPUtility.MapToIcon(file.Web, SPUtility.ConcatUrls(file.Web.Url, file.Url),"", IconSize.Size32));
                                        rootnode.ChildNodes.Add(childNode);
                                        //  rootnode.ChildNodes.Add(TraverseFolder(childFolder, childNode));
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            //TODO: handle error   
                        }
                     return ;
    }
    
             /// <summary>
            /// Open selected folder of the templates library and fill
            /// the data table with the templates in the folder
            /// </summary>
            /// <param name="folderUrl">Target folder</param>
    private void GetTemplatesInSelectedFolder(string folderURL)
    {
     	using (SPSite site = new SPSite("http://test-net.dri.local"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        
                        SPList list = web.Lists.TryGetList("Обрасци за ревизију");
                        if (list != null)
                       {
                            SPDocumentLibrary docLib = (SPDocumentLibrary)list;
                            SPFolder folder = web.GetFolder(folderURL);
                            
                           SPWeb docLibWeb = SPContext.Current.Web;
                           AddFilesToDataTable(docLibWeb, folder);
                       }
                   }
               }
    }
    
              /// <summary>
            /// Add templates to the data source
            /// </summary>
            /// <param name="docWeb"></param>
            /// <param name="folder"></param>
    private void AddFilesToDataTable(SPWeb docWeb,SPFolder folder)
    {
     	 foreach (SPFile file in folder.Files)
               {
                   // get template icon
                   string docIcon = SPUtility.ConcatUrls("/_layouts/images/", SPUtility.MapToIcon(file.Web, SPUtility.ConcatUrls(file.Web.Url, file.Url),"", IconSize.Size32));
    
                   // absolute url of the template
                   string absUrl = (string)file.Item[SPBuiltInFieldId.EncodedAbsUrl];
                   
                   // click action
                   string action;
    
                   if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["DocLib"]))
    
                   {
                       // get the list from the List query string
                       Guid? docLibId =  new Guid(HttpContext.Current.Request.QueryString["DocLib"]);
                    //   string docLibId = HttpContext.Current.Request.QueryString["DocLib"];
                      
                       // create list object and list item object
                    //   SPList selectedLibrary = docWeb.Lists[new Guid(docLibId)];
                     SPList selectedLibrary = docWeb.Lists.GetList(docLibId.Value,true);
    
                       string docLibUrl = selectedLibrary.DefaultViewUrl;
                       // remove Forms[%] from the view url to get the
                       // correct document library url
                       docLibUrl = docLibUrl.Remove(docLibUrl.LastIndexOf("Forms",StringComparison.Ordinal));
                       string[] splitdocLibUrl = docLibUrl.Split('/');
                       SPSite site = new SPSite("http://test-net.dri.local");
                       action = string.Format("javascript:OpenNewTemplate('{0}','{1}');", absUrl, site.RootWeb.Url + '/' + selectedLibrary.Title.ToString() + '/');
                   }
                   else
                   {
                       // if query string DocLib is empty use the default web url
                       action =string.Format("javascript:OpenNewTemplateThenClose('{0}','{1}');",absUrl,SPContext.Current.Web.Url);
                   }
     
    
                  
                   AddDataToDataTable(docIcon, file.Name, action);
    
                   
               }
    }
      /// <summary>
           /// Add template to the data source
           /// </summary>
           /// <param name="docIcon">Template icon</param>
           /// <param name="title">Template title</param>
           /// <param name="url">Template url</param>
    private void AddDataToDataTable(string docIcon,string title,string url)
    {
     	DataRow row = _dataTable.NewRow();
               row["ImageUrl"] = docIcon;
               row["Title"] = title;
                row["Url"] = url;
               _dataTable.Rows.Add(row);
    }
    
                    
                          
             
                          
           
           
          
        }
    }
    

    instead new SPSite("http://test-net.dri.local") i use something else. address second collection is http://test-net.dri.local/sektor3/2020

    • Edited by gogi1000 Wednesday, July 31, 2019 1:31 PM edit
    Wednesday, July 31, 2019 1:27 PM
  • Hi,

    The code snippet below for your reference.

    using SPSite site = new SPSite("http://test.local")
    {
    	using SPWeb web = site.OpenWeb()
    	{
    		using SPSite oSite = new SPSite("http://test.local/sektr3/2020")
    		{
    			using SPWeb oWeb = oSite.OpenWeb()
    			{
    
    			}
    		}
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, August 1, 2019 1:52 AM
    Moderator
  • the problem is when i deploy solution in test environment with address http://test.local
    using SPSite site = new SPSite("http://test.local")
    , it's ok but when i want deploy in production where is address http://example.local i have problem.because i want change http://test.local with something else.
    Thursday, August 1, 2019 5:58 AM
  • Hi,

    Please try to use the address "http://localhost".

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, August 1, 2019 8:31 AM
    Moderator