Upload file to share point server from external application - vb.net

My client has requested to upload files to his share point server from and external application. So I have designed a windows application and used Microsoft.sharepoint.dll supplied by client and used the following code for upload.
Public Function UploadFileToSharepoint(ByVal pstrSourceFilePath As String, ByVal pstrTargeSPURL As String) As Boolean
If Not File.Exists(pstrSourceFilePath) Then
Throw New ArgumentException(String.Format("{0} does not exist", pstrSourceFilePath), "srcUrl")
End If
Dim site As SPWeb = New SPSite(pstrTargeSPURL).OpenWeb()
Dim fStream As FileStream = File.OpenRead(pstrSourceFilePath)
Dim contents(CInt(fStream.Length)) As Byte
fStream.Read(contents, 0, CInt(fStream.Length))
fStream.Close()
EnsureParentFolder(site, pstrTargeSPURL)
site.Files.Add(pstrTargeSPURL, contents)
Return True
End Function
I am able to compile it but while running the application I am getting an error like "Could not load or found an assembly "Microsoft.Sharepoint.Library.dll".
My question: Is it possible to develop an application to create a folder structure and upload the file to share point server without having the share point installed on the machine but having only the share point dll's?
Suggest me a way to carry out this kind of development. In future my application will run on a machine where share point server is not installed.
Rupesh

Yes, you can do that using Client Object Model - just reference Microsoft.SharePoint.Client in your project. Here's how to do it in C#, VB.net shouldn't be much different.
ClientContext context = new ClientContext("http://mydomain");
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(#"C:\MyFile.docx");
newFile.Url = "MyFile.docx";
List docs = web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();

You should look into using the SharePoint Client-Side Object model (CSOM). That will allow you to interact with SharePoint from a client.
More info here --> http://msdn.microsoft.com/en-us/library/office/ee535451(v=office.14).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Related

Determine Documentum host name when using Documentum.Interop.DFC C# assembly

I'm working on some rather old C# code that uses Documentum DFC (Documentum.Interop.DFC.dll 6.5.0.18). The Documentum server settings are stored within the dfc.properties file stored on my local machine, for example:
dfc.docbroker.host[0]=xyzserver.xyzdomain.net
dfc.docbroker.port[0]=5432
I would like to be able to determine, and write to a log file, the Documentum "docbroker" host and port number whenever the Documentum COM objects are instantiated. Here's what I have so far:
DfClientX xClient = new DfClientXClass(); // <=== This xClient should have the host in there somewhere... right?
IDfLoginInfo login = xClient.getLoginInfo();
login.setUser( localUserName );
login.setPassword( localUserPassword );
xClient is an interface of type DfClientX, it is instantiated as a COM object.
Looping through all of the properties of the object suggested by this post looks promising.
I've searched on the Documentum boards with no luck so far. I realize this question will be difficult to answer without having access to the Documentum software, but it seems like it should be a rather simple task... perhaps someone with more general COM knowledge can help out?
Thanks in advance!
So after checking out the object in the Visual Studio watch window (not within the "tooltip" watch), I was able to use Intellisense to examine the properties available to the COM object and its properties. Here is how I am now able to determine the host name:
DfClientX xClient = new DfClientXClass();
IDfClient client = xClient.getLocalClient();
const string HOST_ATTRIBUTE = "dfc.docbroker.host";
var hostSetting = xClient.getLocalClient().getClientConfig().getString( HOST_ATTRIBUTE );
Logging.WriteLog( TraceEventType.Verbose, "*** {0} = {1}", HOST_ATTRIBUTE, hostSetting.ToString() );
Maybe this will help somebody else in the future.

Custom SOAP client DLL exposed as com component using vs2013

One of our clients have an existing access db which connects to our wsoap web service. MS has deprecated the library we once used to access the web service from VBA.
This custom DLL is my only choice other than writing a new app for them which am not authorized to do because of cost.
I've written the following class
namespace MAServicesComClient
{
public class TiRequestComClient
{
public Task<ServiceReference1.SubmitAsXmlResponse> SubmitAsXmlRequest(string username, string password,string xml)
{
ServiceReference1.ServiceSoapClient srv = new ServiceReference1.ServiceSoapClient();
return srv.SubmitAsXmlAsync(username, password, xml);
}
}
}
When I execute that method in VBA it says it can't find a reference to the end point.
I have implemented this class within a console app and it works.
How do I get it to use the configuration file in the same directory as the access db file?
Edit:
It seems to work if I copy the dll file to where the database file is then register it using regasm.exe. Then drop the .dll.config where msaccess.exe is then rename to msaccess.exe.config.
But is there no way I can get to use the config within the same directory as the dll
Quite sure you can FORCE load the config file with this:
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE",ConfigFile);
The configfile in above has to be a full path name. And if you place the .dll in the current folder. So something like this should work:
' intilize event:
Dim strP As String
strP = System.Reflection.Assembly.GetExecutingAssembly.Location
strP = strP & ".config"
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", strP)

Is it necessary that Data Source of connection string must match the system name

This is my first post to this precious website. I am a new learner of vb.net. I am working on a simple purchase project, where i got some errors. But the first thing is which baffled me is:
This is my connection string at module level, on the developed machine.
Public strCn As String = "Data Source = (local); Initial Catalog = PSys; Integrated Security = false; User ID = sa; Password = 123;"
Is it mandatory that Data Source must be the original name of the System Name. I mean If i use (local) or using ( . ), so will it work or not? Because when i copy my project to any other system for further development so every time i need to change the Data source, otherwise i get the error that: "Network-related or instance-specific error occurred......."
Kindly guide me that what i need to do.
When you are developing an application which uses a database server such as MsSQL it is not wise to install the server along with your application in every pc which is installed to. For example what are you going to do if a customer has a local network with 10 computers? Are you going to install SQL server in all 10 of them? And if so what if they need to share data?
So your best approach (based on common practice by other applications) will be to allow the user to install the SQL server where he wants and let him configure your application and point it to the server's location. If you follow that path then the configuration of your application can be in the setup application or in the application itself.
Now about the development phase, I had a similar situation in which I needed to develop the same application in two different computers. What I did was to install the SQL server in both of them with a named instance "sqlexpress" then in the application I used the
Data.SqlClient.SqlConnectionStringBuilder
class to build the connection string. I did something like this:
Public Function getDevConnectionString() As String
Dim csb As New Data.SqlClient.SqlConnectionStringBuilder(My.Settings.dbConnectionString) '<-My original cs in app settings
csb.DataSource = My.Computer.Name & "\sqlexpress"
Return csb.ConnectionString
End Function
Whenever I need a connection string I simply call getDevConnectionString() which returns the connection string based on the computer name plus the sql server instance name. For example:
Dim cs As String
#If DEBUG Then
cs = getDevConnectionString()
#Else
cs = getReleaseConnectionString()
#End If
where getReleaseConnectionString() is the function that returns your connection string configured by the customer.
Hope this point you the right direction...

Featurereceiver sharepoint 2010 xdocument 503

My issue seems to be related to permissions, but I am not sure how to solve it.
In the FeatureActivated event of one of my features I am calling out to a class I created for managing webconfig entries using the SPWebConfigModification class. The class reads up an xml file that I have added to the mapped Layouts folder in the project.
When I deploy the .wsp to my Sharepoint server everything gets installed fine, but when the FeatureActivated event runs it throws a 503 error when attempting to access the xml file.I am deploying the .wsp remotely using a powershell script and I have the powershell, the iisapp pool and the owstimer.exe all using the same domain administrative user.
I assumed the issue was that the FeatureActivated event code was being run within the scope of the OWSTIMER.exe so changed the logon of the service to a domain user that has administrative access to the server to see if that would solve the problem, but no matter what I am getting the 503.
I have traced out the URL to the xml file and pasted that into IE and I am getting back the xml without issue from the server once its copied.
Can anyone give me any idea where to look to figure out why the FeatureActivated event code can't seem to get to the XML file on the server?
Below is the code in my class that is being called from the FeatureActivated event to read the xml.
_contentservice = ContentService;
WriteTraceMessage("Getting SPFeatureProperties", TraceSeverity.Medium, 5);
_siteurl = properties.Definition.Properties["SiteUrl"].Value;
_foldername = properties.Definition.Properties["FolderName"].Value;
_filename = properties.Definition.Properties["FileName"].Value;
_sitepath = properties.Definition.Properties["SitePath"].Value;
WriteTraceMessage("Loading xml from layouts for configuration keys", TraceSeverity.Medium, 6);
xdoc = new XDocument();
XmlUrlResolver resolver = new XmlUrlResolver();
XmlReaderSettings settings = new XmlReaderSettings();
StringBuilder sb = new StringBuilder();
sb.Append(_siteurl).Append("_layouts").Append("/").Append(_foldername).Append("/").Append(_filename);
WriteTraceMessage("Path to XML: " + sb.ToString(), TraceSeverity.Medium, 7);
WriteTraceMessage("Credentials for xml reader: " + CredentialCache.DefaultCredentials.ToString(), TraceSeverity.Medium, 8);
resolver.Credentials = CredentialCache.DefaultCredentials; //this the issue might be here
settings.XmlResolver = resolver;
xdoc = XDocument.Load(XmlReader.Create(sb.ToString(), settings));
I finally punted on this issue because I discovered that while adding the -Force switch to the Enable-SPFeature command did use a different process to activate the feature when adding a solution it did not work when updating a solution. Ultimately I just changed my XDocument.Load() to use a TextReader instead of a URI. The xml file will always be available when deploying the WSP because it is part of the package so there is no reason to use IIS and a webrequest to load up the xml.

Create a virtual directory in iis 6 programatically using vb.net as unprivileged user

I'm trying to write an application that will allow a non-administrator user to create virtual directories in IIS6. I've tried numerous different ways of impersonating a administrative account but nothing seems to work to create the virtual directory.
Here's some of what I've tried:
dim sDirPath as string = "IIS://remotehost/W3svc/1/root"
Dim de As DirectoryEntry = New DirectoryEntry()
de.Path = sDirPath
de.AuthenticationType = AuthenticationTypes.Secure
de.Username = tbxUsername.Text
de.Password = tbxPassword.Text
''Fails here
virtualId = CType(de.Invoke("Create", "IIsWebVirtualDir", dirName), DirectoryEntry)
Using the AccountAlias class from here:
aa.BeginImpersonation()
Try
dim sDirPath as string = "IIS://remotehost/W3svc/1/root"
Dim de As DirectoryEntry = New DirectoryEntry()
de.Path = sDirPath
de.AuthenticationType = AuthenticationTypes.Secure
''Fails Here
virtualId = CType(de.Invoke("Create", "IIsWebVirtualDir", dirName), DirectoryEntry)
Finally
aa.EndImpersonation()
End Try
what is the correct way to impersonate another user in winforms application that will allow me to create the virtual directory WITHOUT the user that's running the application being an administrator on the web server? I know this can be done, IIS manager allows you to use "connect as" checkbox to do it.
It's been a long time since I hit this problem so I may be remembering a few things wrong. But IIRC ...
The root directory of IIS is protected by an ACL that requires administrative privs in order to create a new directory. A simple impersonation likely will not fix this issue because I believe under the hood you are combining both COM and either RPC or Local RPC. Impersonation across COM boundaries is tricky in native code and very tricky from managed code. It's very likley that you are running into an issue in this area with your impersonation trick.
It is possible though to lift the restriction on the web server and have normal users create root directories in IIS. I outlined the process to do so in the following blog post
http://blogs.msdn.com/jaredpar/archive/2005/02/04/367137.aspx