Friday, September 23, 2005 1:17 AM bart

About "My Network Places", Nethood, target.lnk and attrib

Today I've been playing around to find a mechanism to update the My Network Places folder in a network environment to contain links to all SharePoint Document Libraries on a given server. I still have to put pieces together and fill in missing links, but here are some frustrating things I've been sailing around :o. Notice that all of the following only works on a machine with Office 2003 (maybe earlier releases too) to introduce the notion of "web folders" in the shell.

1. My Network Places contains links to - guess what - network locations. These are stored in %HOMEPATH%\Nethood under a user's profile in the form of folders (which have the name of the link that appears in the list in Windows Explorer) containing two files: target.lnk and desktop.ini

2. target.lnk is the hardest one to get right. Up till now, I've not found a way to create such a link programmatically (doing a lot of P/Invoke and interop stuff in C#). The pieces of the puzzle are called IShellLink, it's method SetIDList, PIDL and SHGetFolderLocation and CSIDL_NETHOOD. If someone finds a way to create a .lnk file to a web folder using these pieces of the puzzle, let me know (C# managed code preferred, but WSH/VBScript or C++ solutions are also welcome). In most cases, the file was renamed to a .url file which is handled by IE.

3. desktop.ini contains the following:

[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Flags=2

4. Retrieving all document libraries in all subwebs of a given SharePoint site was a piece of cake using the SharePoint .asmx files. You can find the code of my "crawler" over here in a zip file: sharepointwebcrawler.zip. It retrieves the title and url of every document library it can find recursively. Please notice this code can be improved further, especially the tricky part to extract the document library's base URL.

5. Once you have the list of all document libraries (URL + title) you need to create the target.lnk files and the folders. Right now, this is the missing link in my solution (see point 2 above) and requires manual interaction.

6. Deploy the created .lnk files to all target machines. If you thought it was as easy as doing xcopy of the entire Nethood folder, you're wrong. Okay, it's basically that but first you should not forget to copy the hidden desktop.ini file too, and second, all folders need to be marked as read-only (otherwise these don't appear as "shortcuts" but as normal folders containing a target.lnk file). I did end up with something like this:

cd %USERPROFILE%\Nethood
xcopy /E /H %SOURCEDIR% .
attrib +R /D /S .*

What a nightmare :s. Dreaming of .lnk files to become XML-files :-).

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# re: About "My Network Places", Nethood, target.lnk and attrib

Friday, September 23, 2005 12:08 PM by bart

Hi Bart,

i'm sure this one will help you. It should be easily portable to C#.

http://vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/Creating_and_Modifying_Shortcuts/article.asp

# re: About "My Network Places", Nethood, target.lnk and attrib

Friday, September 23, 2005 12:10 PM by bart

Oops, it's already in C# :)

# re: About "My Network Places", Nethood, target.lnk and attrib

Friday, September 23, 2005 12:21 PM by bart

I thought a bit about this.. alternatively you could write your own Shell Namespace. You would have full control on what and how and where to show in explorer.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_adv/namespaceextension/namespace.asp

# re: About "My Network Places", Nethood, target.lnk and attrib

Wednesday, October 19, 2005 2:02 PM by bart

I've searched on tons of places for how to create these links. Until I had it and coded my own solution. The target.lnk is actually a normal shortcut. So this one is scriptable.

I even made a 1.0 version of a VBS-script to do ALL what you need to make these special folders. Hope you can use it:


'*************************************************************************
'* File: EditResource_1.0.vbs
'* Description: Creates or Deletes resources in "My network places" using input parameters
'* For more info, run the file without paramers
'* A failed run creates ERROR-events in Eventviewer
'*
'* Requirements: WSH 5.5 or higher.
'* Created date: 2005-10-18
'*
'* Last modified: XXXX-XX-XX
'* Last modfied by: <name>
'* Information: ....
'*
'*************************************************************************
Option Explicit
Dim strPath, strName, strAppname, strNetHood, blnDeleteMode, objFSO, objWSHShell, objShell, objFolder, objFolderItem, blnVerboseMode

'Set version and name of script (should be the same as script-name)
strAppname = "EditResource_1.0.vbs"
Const NETHOOD = &H13&

'Create objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")

'Set vars
strPath = ""
strName = ""
blnDeleteMode = false
Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self

'Set this VAR to true to get MsgBox Errors instead of eventvwr-errors
blnVerboseMode = false


'Get inputs
Call GetParams()

'Create resource OR Delete all resource depending on parameter-input /DELETE
If blnDeleteMode = True then
Call EraseResources()
Else
Call CreateResource()
End If



'Clean up
Set objShell = Nothing
Set objFSO = Nothing
Set objWSHShell = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing







'Creates a resource in nethood
Sub CreateResource()

'Set vars to create resource
strNetHood = objFolderItem.Path

'Create resource
Dim objShortcut, objDesktopIni, objNewLinkFolder, strIniFileContent

'Don't create shortcut if an existing folder is already is named the same way
If NOT objFSO.FolderExists(strNetHood & "\" & strName) AND objFSO.FolderExists(strNetHood) then

'Create the main folder
Set objNewLinkFolder = objFSO.CreateFolder(strNetHood & "\" & strName)

'Create a desktop.ini below that folder
Set objDesktopIni = objFSO.CreateTextFile(strNetHood & "\" & strName & "\Desktop.ini")

'Set the content of Desktop.ini
strIniFileContent = "[.ShellClassInfo]" & vbNewLine &_
"CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}" & vbNewLine &_
"Flags=2" & vbNewLine &_
"ConfirmFileOp=1"

'Write content to INI-file
objDesktopIni.Write strIniFileContent
objDesktopIni.Close

'Reopen Desktop.ini as file-object to set attribs
Set objDesktopIni = objFSO.GetFile(strNetHood & "\" & strName & "\Desktop.ini")

'Create target.lnk (used to point where the system-folder-path shall be)
Set objShortcut = objWSHShell.CreateShortcut (strNetHood & "\" & strName & "\target.lnk")
objShortcut.TargetPath = strPath
objShortcut.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85"
objShortcut.Description = strPath
objShortcut.WorkingDirectory = strPath

'Save shortcut to NETHOOD
On Error Resume Next
objShortcut.Save

'After everything is created - Set required Attribs on the folders / files
objDesktopIni.Attributes = 6 'Hidden and System
objNewLinkFolder.Attributes = 1 'Read only

'Clean up
Set objShortcut = nothing
Set objNewLinkFolder = nothing
Set objDesktopIni = nothing

'If Error - Create an eventlog containing the error
If Err.Number <> 0 then
objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & "Cannot create resource:" & strPath & vbNewLine & "Error detail:" & Err.Description
End If

End If

End Sub




'Empties all the resources from nethood
Sub EraseResources()

Dim oFolder, oSubFolders, oCurrentSubFolder

'Remove all links created as files
strPath = objFolderItem.Path & "\*.*"
On Error Resume Next
objFSO.DeleteFile strPath, true

'If Error - Create an eventlog containing the error
If Err.Number <> 0 then
objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & "Cannot empty folder of files:" & strPath & vbNewLine & "Error detail:" & Err.Description
End If

'Some shortcuts are folders, loop and delete all of them too
Set oFolder = objFSO.GetFolder(objFolderItem.Path)
Set oSubFolders= oFolder.SubFolders

For Each oCurrentSubFolder in oSubFolders
On Error Resume Next
objFSO.DeleteFolder oCurrentSubFolder, true

'If Error - Create an eventlog containing the error
If Err.Number <> 0 then
objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & "Cannot empty folder of subfolders:" & strPath & vbNewLine & "Error detail:" & Err.Description
End If
Next


End Sub





'Get's parameters supplied, and checks if valid
Sub GetParams()

Dim objArgs
Set objArgs = WScript.Arguments

If objArgs.Count = 0 Then
Call HelpText()
Wscript.Quit(0)
Else

Dim i, intArgCount, strTempParse, intRequiredParameters, currentArg, errMessage

'Count number of args passed to script
intArgCount = objArgs.Count - 1

'Set how many args that are required, decrease this value with 1 each time a required value is valid and set
intRequiredParameters = 2

For i = 0 to intArgCount
currentArg = objArgs(i)

If Ucase(currentArg) = "/PATH" then

'Control that the value for this parameter exists
If intArgCount >= i + 1 then

'Control that it is valid
If objFSO.FolderExists(objArgs(i + 1)) then

'Get the value of the valid parameter
strPath = objArgs(i + 1)

'Mark that 1 required parameter is set
intRequiredParameters = intRequiredParameters - 1

Else
errMessage = GetParamsBuildError(currentArg, errMessage, " - Folder does not exist or no access to it: " & objArgs(i + 1))
End If

Else
errMessage = GetParamsBuildError(currentArg, errMessage, " - No value for parameter")
End If


ElseIf Ucase(currentArg) = "/NAME" then

'Control that the value for this parameter exists
If intArgCount >= i + 1 then

'Control that it is valid
If len(objArgs(i + 1)) > 0 then

'Get the value of the valid parameter
strName = objArgs(i + 1)

'Mark that 1 required parameter is set
intRequiredParameters = intRequiredParameters - 1

Else
errMessage = GetParamsBuildError(currentArg, errMessage, " - No name specified")
End If

Else
errMessage = GetParamsBuildError(currentArg, errMessage, " - No value for parameter")
End If

ElseIf Ucase(currentArg) = "/DELETE" then

'Set deletemode, and ignore the rest of the parameters
blnDeleteMode = true

errMessage = ""
intRequiredParameters = 0
Exit For

End If

Next



'If any error or missing parameters display error to user
If len(errMessage) > 0 OR NOT intRequiredParameters = 0 then

If NOT intRequiredParameters = 0 then
errMessage = GetParamsBuildError("", errMessage, vbNewLine & " - Not all required parameters specified or valid")
End If

If blnVerboseMode = true then

'Create a Messagebox containing the error
Dim intSeeHelp
intSeeHelp = MsgBox("Parameter Errors:" & vbNewLine & errMessage & vbNewline & vbNewline & "Do you want to see the help?", 308, "Error in parameters")

If intSeeHelp = 6 then
HelpText()
End If

Else
'Create an eventlog containing the error
objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & errMessage
End If

'Quit with error-code -1
Wscript.Quit(-1)

End If
End If

End Sub



'Displays help-text
Function HelpText()

MsgBox "Required parameters:" & vbNewLine & vbNewLine &_
" - /NAME ""<Resource name>""" & vbNewLine &_
" - /PATH ""<Path to resource>""" & vbNewLine & vbNewLine &_
"--- OR ---" & vbNewLine & vbNewLine &_
" - /DELETE" & vbNewLine &_
" - Deletes ALL resources in My network places (NETHOOD)" & vbNewLine &_
" - When using this command, script ignores /NAME and /PATH parameters, and will not create any new resource" & vbNewLine & vbNewLine &_
"Example:" & vbNewLine &_
strAppname & " /NAME ""Delade wordfiler"" /PATH ""\\sbab.ad\dfs\DeladeWordfiler""" & vbNewLine &_
strAppname & " /DELETE" & vbNewLine &_
"", vbInformation, strAppname & " - Required parameters"

End Function



'Builds errorstring when parsing parameters
Function GetParamsBuildError(strArgument, errMessage, strErrortxt)
GetParamsBuildError = errMessage &_
Ucase(strArgument) &_
strErrortxt &_
vbNewLine
End Function

# re: About "My Network Places", Nethood, target.lnk and attrib

Thursday, October 27, 2005 11:55 PM by bart

Dude, you are the man. Unbelievable!!!! Thank you very much!!!!!!

# re: About "My Network Places", Nethood, target.lnk and attrib

Sunday, April 23, 2006 5:59 PM by Shrini Viswanathan

Has any one found any automated solution for adding SP DocLib Links into My Network Places?

I currently create the link in one machine, xcopy the links. Then when I copy it to the other machine and set the folder to readonly it works.

# Network locations for Windows 7 &#187; Compower

Thursday, April 12, 2012 3:12 PM by Network locations for Windows 7 » Compower

Pingback from  Network locations for Windows 7 &#187; Compower

# Network locations for Windows 7 &#187; Compower

Thursday, April 12, 2012 3:12 PM by Network locations for Windows 7 » Compower

Pingback from  Network locations for Windows 7 &#187; Compower