Sitecore Cross linking of assets in multiple sites - cross-domain

I have following tree structure in Sitecore 7
Content
Site 1
page 1
Site 2
page 1
Media Library
Site 1 media files
pdf
1.pdf
Site 2 media files
pdf
1.pdf
Media library is a shared library and looks like user can access site 2 media files from site 1 and site 1 media files from site 2, Can I stop this behavior?
I want if I reference site 2 files from site 1 pages, generated link should open them from site 2 domain.
for example currently If I am browsing page 1 of site 1 and that has a link to 1.pdf of site 2, following link is generated
http://site1/~media/site2/pdf/1.pdf
link is working properly but site 2 files is being served by site 1 domain
I want url of the file should be as following
http://site2/~media/site2/pdf/1.pdf
Node:I have a custom link provider and that works very well for pages across the sites.

You can create your custom Media Provider and register it instead of default one:
<mediaLibrary>
<mediaProvider type="My.Assembly.Namespace.CustomMediaProvider, My.Assembly" />
</mediaLibrary>
public class CustomMediaProvider : MediaProvider
{
public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
{
string mediaUrl = base.GetMediaUrl(item, options);
if (mediaUrl == null || mediaUrl.StartsWith("http"))
{
return mediaUrl;
}
string[] parts = mediaUrl.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 2)
{
// skip "~" and "media" parts
string mediaLibraryTopFolder = parts[2];
// assuming that folder names are site1 and site2
SiteInfo siteInfo = SiteContextFactory.GetSiteInfo(mediaLibraryTopFolder);
if (siteInfo != null)
{
SiteContext siteContext = new SiteContext(siteInfo);
// change logic if you need https
mediaUrl = "http://" + siteContext.HostName + "/" + mediaUrl.TrimStart('/');
}
}
return mediaUrl;
}
}
This code is not tested, but if the media item link does not contain host name yet, it should get the top level folder from the media path, get the site with same name as folder name and add proper host name at the beginning of the path.
Remember that this will change all your media links so they will include http and host name and that this will work only for links generated by the MediaManager or MediaProvider.

Related

Can the child documents inherit the Custom URL path of it's parent in Kentico

I have a situation where my client wants to have SEO friendly URLs for their documents and for these URLs to flow down to the client documents. Here is an example setup:
Root
Groups
Group1 (custom url=/groups/ma/salem/group1)
Page1
Page2
Group2 (custom url=/groups/ma/boston/group2)
Page2
Page4
etc.
The would like Page1, Page2, Page3, Page4 to inherit it's parent's custom url and be:
/groups/ma/salem/group1/page1
/groups/ma/salem/group1/page2
/groups/ma/boston/group2/page2
/groups/ma/boston/group2/page3
When I set the Custom URL path it only affects that document and the child documents stay the same:
/groups/group1/page1
/groups/group1/page2
/groups/group2/page2
/groups/group2/page3
Can this be achieved in Kentico without modifying the tree structure to contain the URL parts?
Is there a way to override the ResolveURL() function so i can return the SEO friendly URL?
I'm using Kentico 8.1
Creating those document in the tree would be definitely the easiest and safest solution, but taken you want to avoid this I see two other options.
1) Create URL rewriting rules to simulate this tree hierarchy
2) Catch document insert before event and set the custom URL path according your needs.
Code could look something like this:
DocumentEvents.Insert.Before += DocumentInsert_Before;
private static void DocumentInsert_Before(object sender, DocumentEventArgs e)
{
TreeNode node = e.Node;
if (node.NodeAliasPath.StartsWith("/groups/group1")) {
string safeNodeName = TreePathUtils.GetSafeDocumentName(node.DocumentName, CMSContext.CurrentSiteName);
string customPath = "/groups/ma/salem/group1/" + safeNodeName;
// Handle multiple dashes
customPath = TreePathUtils.GetSafeUrlPath(path, CMSContext.CurrentSiteName, true);
node.DocumentUrlPath = customPath;
}
}

how to get home page url in orchard cms

I am building module to register customers and after registration i need to redirect the user to home page (default). I cant see a way as in Orchard everything works as content items.
Some of my code from Controller is given below
$ if (!ModelState.IsValid)
return new ShapeResult(this, _services.New.Checkout_Signup(Signup: signup));
var customer = _customerService.CreateCustomer(signup.Email, signup.Password);
customer.FirstName = signup.FirstName;
customer.LastName = signup.LastName;
customer.Title = signup.Title;
_authenticationService.SignIn(customer.User, true);
return Redirect("~/Home Page URL here...");
In Orchard, the home page has an empty string for its alias. It's possible to look up the RouteValueDictionary of an alias by calling the IAliasService.Get() method. Once you have this, you can simply pass it to RedirectToRoute().
So for the home page:
var homepage = _aliasService.Get(String.Empty);
return RedirectToRoute(homepage);
You can see Orchard using this mechanism to check the home page in the AutoroutePartDriver.cs file lines 66 - 72 in version 1.7.2.

SharePoint Get Site Collections for Current User

We have a SharePoint Web Application that has a number of Site Collections underneath 2 different managed paths (depts & offices) e.g
http://sharepoint.abc/depts/finance
http://sharepoint.abc/depts/isg
http://sharepoint.abc/offices/boston
http://sharepoint.abc/offices/chicago
When a user logs in they are presented with a list of the site collections they have read access to using the following c# code which is in the WebPart
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
{
foreach (SPSite site in spSite.WebApplication.Sites)
{
try
{
var rootWeb = site.RootWeb;
if (rootWeb.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.ViewPages))
{
if (this.ValidSite(rootWeb.Url))
{
string url = GetRelativePath(rootWeb.Url);
allowedSites.Add(new SiteInfo(rootWeb.Title, url));
}
}
}
catch (Exception ex)
{
this.Controls.Add(new LiteralControl("<br/>GetAllowedSites Error: " + ex.Message));
}
}
}
});
It works fine but in production it takes 20-seconds to load the webpart (we have 700 site collections across the 2 paths).
I've used caching to hold the list of their sites but once the cache expires it takes 20-seconds to regenerate itself.
Ideally what I want is to see what Site Collections a user can access using the User rather than iterating through all the Site Collections to see if the user has access to them. Can this be achieved???
Thanks
eaigs
Try to use SPWeb.GetSubwebsForCurrentUser method to get the subsites beneath the current website of which the current user is a member.

How do you get SharePoint "My Links" back after a profile has been deleted and restored?

We're running SharePoint 2007 SP1 and profiles are imported from Active Directory (a full import runs daily). We had a problem where many of the users were disabled unintentionally in Active Directory and this caused their profiles to be removed from SharePoint. We re-enabled their Active Directory accounts and ran a full import which restored their SharePoint profiles. However, all of their My Links are missing. Is there a method or best practice for restoring them?
I posted this because I couldn't find an answer to my problem anywhere. This post by Joel Oleson that describes a similar problem to mine gave me a hint as to where to go looking for the missing data. And This post by Corey Roth showed me how to programatically add the links to a users My Links.
First things first - you need to restore a backup of the database that contains the My Links data. You don't want to restore over your working database, you want to restore it to another location. The links stored in the SSP Database. (You can find out the name of the database by going into Central Admin --> Shared Services Admin then open the menu for the SSP and click on Edit Properties - the SSP Database is listed on the properties page.)
Once the database has been restored you want to retrieve the Link information:
the domain account name of the user who owns the link,
the url of the link
the name of the link
This query will get you that information:
SELECT UPF.NTName, UL.Url, UL.Title
FROM UserLinks UL INNER JOIN UserProfile_full UPF ON UL.recordID = UPF.recordID
INNER JOIN UserPrivacyPolicy UPP ON UL.PolicyId = UPP.id
ORDER BY NTName
(I should note that I did not take into account what group or what privacy level the links were set to, you could probably find that information by looking at the information in the UserPrivacyPolicy table)
I copied the results into Excel & saved it as a .csv file (comma separated list) - just because my production server did not have access to the location where I restored my database. I ordered the columns with Title last because the Title could contain commas and that would mess up how I'm reading in the data. (I checked and the other two fields do not contain commas - you should check yours before making this assumption.)
I then wrote a little console app to import the data. It takes two arguments:
the path where the file containing all of the links is located (ie c:\temp\links.csv)
the url of the SSP from with the My Links have gone missing (ie https://portal.mydomain.com)
These are the references used:
Microsoft.Office.Server (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.Office.Server.dll)
Microsoft.SharePoint (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll)
System
System.Data
System.Web
System.Xml
And this is the code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web;
namespace UserLinks
{
class Program
{
static void Main(string[] args)
{
string _accountName = "", _linkTitle = "", _url = "", _tmp = "", _path = "", _SPSsite = "";
// Check arguments
if (args.Length != 2)
{
ShowUsage();
return;
}
_path = args[0];
_SPSsite = args[1];
using (SPSite _site = new SPSite(_SPSsite))
{
ServerContext _context = ServerContext.GetContext(_site);
UserProfileManager _userProfileManger = new UserProfileManager(_context);
/* Expecting a comma seperated list with 3 columns:
* AccountName in the format Domain\Account name - I am assuming there are no commas in this field
* URL - I am assuming there are no commas in this field
* Link Title - link title is last because there may be commas in the title
*/
TextReader _reader = new StreamReader(_path, System.Text.Encoding.Default);
while (_reader.Peek() != -1)
{
_tmp = _reader.ReadLine();
_accountName = _tmp.Substring(0, _tmp.IndexOf(','));
_tmp = _tmp.Replace(_accountName + ",", "");
_url = _tmp.Substring(0, _tmp.IndexOf(','));
_linkTitle = _tmp.Replace(_url + ",", "");
try
{
UserProfile _currentUser = _userProfileManger.GetUserProfile(_accountName);
QuickLinkManager _quickLinkManager = _currentUser.QuickLinks;
_quickLinkManager.Create(_linkTitle, _url, QuickLinkGroupType.General, null, Privacy.Private); //I am assuming that all links have no group assigned to them and they are all private links
}
catch (Exception ex)
{
Console.WriteLine(_accountName);
Console.WriteLine(ex);
}
}
_reader.Close();
}
}
private static void ShowUsage()
{
Console.WriteLine("Usage");
Console.WriteLine("UserLinks [FilePath] [SharePoint URL]");
}
}
}
So problem solved & as a side benefit, this program can be used to force links to show up in a user's My Links list.
This post has some pretty good information about MyLinks and its relationship with the SSP database (that's actually where these links are stored counterintuitively.) Hopefully you can have your DBA validate that these links still exist; and that they're associated with the correct profiles.
http://www.k2distillery.com/2009/01/moving-sharepoint-my-links-between-ssps.html
When you do a profile import, you normally risk losing the existing customization/updated information.

How to change web part property values

In a SharePoint 2007 web part, I want to delete an existing property and replace it with a property using a different name. I want to get the value from the existing property and assign it to the new property.
How should I do this?
In summary:
Get a reference to the page containing the web part.
Get a reference to the web part itself.
Change the property value.
Save the change.
In code:
using (SPSite site = new SPSite("http://sharepoint"))
using (SPWeb web = site.OpenWeb("Web Title"))
using (SPLimitedWebPartManager webPartManager =
web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if ((webPart.Title == "Web Part Title") && (!webPart.IsClosed))
{
YourWebPart wp = (YourWebPart)webPart;
wp.NewProperty = wp.OldProperty;
webPartManager.SaveChanges(wp);
web.Update();
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
Replace the following in this code example:
"http://sharepoint" - the address of your SharePoint site
"Web Title" - the title of the SharePoint web containing the web part to be changed (or use one of the other OpenWeb overloads
"default.aspx" - filename of the page containing the web parts
"Web Part Title" - title given to the web part on the page
YourWebPart - class name of the web part to change
NewProperty/OldProperty - names of the properties to change

Resources