How I can upload a file with metadata creating a new version from ASP.NET web application to SharePoint 2010?
Is there any way?
Thanks in advance.
There are multiple ways to upload a file to a document library.
Perhaps the simplest way is simply to copy the file to the document library's URL using File.Copy. SharePoint supports WebDAV so you can treat a library as a network folder, opening/saving files directly from any application, copying and renaming from Windows Explorer etc.
You do face some limitations though:
The full path of the file can't be larger than 260 characters and SharePoint will truncate it without warning.
The file name can't contain some characters that are valid for Windows Explorer, eg #
If you try to save a file with the same name as an existing one, SharePoint will create a new file with a slightly different name without asking
You have no control over the author and creation date properties. SharePoint will use the current date and the credentials of the user executing the code.
You can't set any metadata although you can update the file's ListItem properties after uploading.
Another option is to use the Client object model and upload the file using File.SaveBinaryDirect. This option is better than simply copying the file, as it allows you to overwrite an existing file, but you still don't get direct access to the file's properties. Uploading could be as simple as this:
var clientContext = new ClientContext("http://intranet.contoso.com");
using (var fileStream =new FileStream("NewDocument.docx", FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext,
"/Shared Documents/NewDocument.docx", fileStream, true);
Another option is to connect to a library using the Client Object Model and then create a file using FileCollection.Add, eg
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);
Check this SO question, Upload a document to a SharePoint list from Client Side Object Model on how to use both SaveBinaryDirect and FileCollection.Add. The answers display both how to upload a file and how to modify its properties
When i added service reference to my .Net project (right click->Add Service Reference), VS created a folder with the Service name and created bunch of files (listed below). I am not sure which files should be added to the source control so that next time when i upgrade the service reference, i dont end up deleting some files and adding new files. List of files that VS created in the service folder are as below:
ConfigurationService.disco
ConfigurationService.wsdl
ConfigurationService.xsd
ConfigurationService1.xsd
ConfigurationService2.xsd
ConfigurationService1.wsdl
ConfigurationService3.xsd
ConfigurationService4.xsd
configuration91.svcinfo
configuration.svcinfo
Reference.svcmap
Reference.cs
abc.datasource
def.datasource
ghi.datasource
jkl.datasource
mno.datasource
pqr.datasource
usually we add the whole folder with all its files, you can safely do that.
user specific files are not created in that subfolder.
Consider that when you will update the reference some files will be edited and others will not be touched. If some are fully regenerated TFS or the source control system you are using will get the changes, not the deletion and re-add as another file with same name; at least I would expect so and never seen anything different happening.
I basically have quite a large site collection with various site and sub sites that all contain their own document libraries. I need to change the default view of each document library to include the following fields:
Checked out to.
Check in comments.
This is ok as I have written an app that will loop through all existing lists and do this however is there a way I can change the template for a document library so that any future lists that are created will automatically contain these two fields in the their default view?
I am using WSS 3.0.
Thanks
In WSS 3.0 there is no supported way to modify the out of the box document library
If you don't mind the risk of your change being overwritten by updates then you can modify
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\DocumentLibrary\DocLib\schema.xml"
In SPF 2010 you can make your changes in a ListAdded event receiver.
I have one of those annoying problems where something that used to work stopped working.
Check out this code:
Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
ABC.ContentAttribute attribute;
attribute = (ABC.ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute is defined in the dll.
Obviously, this should work. You should be able to cast an object to itself.
But it produces this error:
alt text http://www.yart.com.au/stackoverflow/compile1.png
This bug is discussed here http://www.yoda.arachsys.com/csharp/plugin.html which is how I got even this far. From this post I gather that the class ContentAttribute is somehow ending up in ABC.DLL and the website project's DLL.
The website project I have looks like this:
alt text http://www.yart.com.au/stackoverflow/compile2.png
Now ContentAttribute is not in this project, it is in the dll ABC.DLL. You can see that as I have expanded every branch and the file ContentAttribute.cs is not there.
Yet somehow it is ending up in the dll for the website creating a duplicating reference. ContentAttribute is somehow ending up in ABC.DLL and the website project's DLL.
Can anyone tell me:
a) Why is ContentAttribute in two dlls? I didn’t think including a dll in a project forced that code into the projects DLL.
b) How to stop it from happening?
By the way, I definitely don't want to change the website project into a website application if I can avoid it.
Notes:
Deleting the temporary ASP.NET files does not work. As soon as I compile my website project they get recreated.
alt text http://www.yart.com.au/stackoverflow/compile3.png
It's a namespace collision. It doesnt know which ContentAttribute to use since it is finding 2 in different namespaces/assemblies.
You may have an old copy of the DLL named differently. Delete your temporary ASP.Net directories and recompile.
To avoid in the future:
Use fully qualified names for your objects if you need to get it to work.
ABC.ContentAttribute ca = new ABC.ContentAttribute();
or if casting do the same
ABC.ContentAttribute ca =(ABC.ContentAttribute)ca2;
ABC.DLL is referenced by your website and it becomes part of it (it is in Bin folder). ASP.NET compiles your website and ABC.DLL is placed in temp location (C:\Windows...\Temporary ASP.NET Filse...). It gets loaded by ASP.NET automatically. You are trying to load ABC.DLL manually from different location (D:\junk\abcabstract\bin\abc.dl). Two assemblies do not match hence you get the error.
To stop this from happening you have to rethink your plugin architecture I guess. Can you give more information?
Update:
Why don't you fix it like this:
// Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
// ContentAttribute attribute;
// attribute = (ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute attribute = new ContentAttribute();
I have an ASP.NET 3.5 Website (visual studio lingo), but the site continues to grow and is looking rather cowboyish among other things. I'd like to see this get converted into a Web Application (namespaces and all).
Is this something that can be easily done in Visual Studio? If not, are there any other tools out there that could create all of the namespaces, etc. automagically?
Well, it turns out that the option "Convert to web application" does NOT exist for "websites". The option "Convert to web application" does exist only for "web applications" !!!!
[emphasis mine]
So, here's the deal, to do the
conversion, you need to:
Add a new "Web Application" to your VS 2008 solution (File->Add->New
Project->C#->Web->ASP.NET Web
Application).
Afterwards, you copy all the files in the old "website" to your newly
created "web application", and
override any files created in it by
default
The next step is the most ugly, you need to "manually" add the references
in your "website" to the new "web
application". I thought the VS 2008
PowerCommands toy would do this for me
as it does copy references from other
project types, but it didn't. You have
to do it by yourself, manually, and
you have to be cautious in this step
if you have multiple versions of the
same assembly (like AJAXToolkit in my
case) or assemblies that have both GAC
and local versions or so.
Keep repeating the last step and trying to build the "web application".
You'll keep getting errors like "
'....' is unknown namespace. Are you
missing an assembly reference? ". Make
sure you have none of those except the
ones where '....' is replaced by the
IDs of the server controls you use. In
other words, keep adding references
and building the project until only
the errors that exist because of
missing .DESIGNER.CS or .DESIGNER.VB
files.
Afterwards, go to the "web application" root project node in VS
2008 solution explorer, and right
click it, then you WILL find the
option "Convert to web application".
What this option does is actually
making small changes to the "#Page"
and "#Control" directives of pages and
controls, and creating the required
.DESIGNER.CS or .DESIGNER.VB files.
Try building the "web application" again. If you get errors, see what
references may be missing and/or go
click the "Convert to web application"
again. Sometimes, if there's any error
other than those caused of missing
DESIGNER files, not all the
pages/controls will have those
DESIGNER files created for them.
Fixing the non DESIGNER problem and
clicking "Convert to web application"
again should do the job for this.
Once you are done successful VS build, you should be ready to go.
Start testing your web application.
Optionally, you can right click the
"web application" root project node in
VS 2008 Solution Explorer and click
"Properties" then go to the tab "Web"
to set the "web application" to a
virtual folder in IIS (you can create
new virtual directory from there in
VS). If you want to use the IIS
virtual directory that the old
"website" used, you need to remove
that from IIS first.
Update: When testing your pages, pay MOST ATTENTION to classes in
"App_Code" folder, especially those
with NO NAMESPACE. Those can be a big
trap. We had a problem with two
extension method overloads in the same
static class that had no namespace,one
extends DateTime? (Nullable)
and calls another overload that
extends DateTime itself. Calling the
other overload as extension method
passed VS 2008 compilation and gave us
a compilation error ONLY IN RUNTIME
(With IIS). Changing the call to the
other overload from calling it as
extension method to calling it as
normal static method (only changing
the call in the same class, calls from
other classes remained extension
method calls) did solve this one, but
clearly, it's not as safe as it used
to be in VS 2005. Especially with
classes with no namespaces.
Update2: During the conversion, VS 2008 renames your "App_Code" to
"Old_App_Code". This new name sounds
ugly, but DO NOT RENAME IT BACK. In
the "web application" model, all code
will be in one assembly. In runtime,
the web server does not know what web
project type you are using. It does
take all code in "App_Code" folder and
create a new assembly for it. This
way, if you have code in folder named
"App_Code", you'll end up with RUNTIME
compilation errors that the same types
exist in two assemblies, the one
created by VS, and the one created by
IIS / ASP.NET Development Server. To
avoid that. leave the "Old_App_Code"
with the same name, or rename it to
ANYTHING EXCEPT: "App_Code". Do not
place any code in such "App_Code"
folder and prefereably do NOT have a
folder with such name in your "web
application" at all.
I know this since before but forgot it
now as I have not used "website" model
for long :(.
Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio at MSDN
If your website application grows.. it's better to split it into several projects. Conversion from Web Site project to Web Application project won't help much.
If you're having problems getting your new Web Application Project to build check the File Properties in Visual Studio of all 'helper' classes. For a project I was converting the Build Action was set to Content whereas it should have been Compile.
I've now successfully migrated one Website project to a web application and there is quiet a few gotchas to look out for.
Having ReSharper at your disposal helps a lot in refactoring the aspx files.
Set up your solution and create an empty WebApplication
Copy all file over
aspx files in website projects don't have a namspace. Wrap your classes in the appropriate namespaces
During copying, all my pages in subfolders got renamed to my project name and the foldername, so I got 40ish public partial class FolderName_Projectname : Page If neccessary rename all files using Resharper or manually.
If you encounter multiple errors like "There is already a member Page_Load() defined", this is most likely due to incorrect class names und duplication
After adding a namespace
Replace CodeFile in all aspx pages with Codebehind and especially pay attention to files i your subfolder. Make sure Inhertis="" doesn't contain the relative path. Your namespaces take care of everything. So the correct format is Inherits="Namespace.classname".
If your class has a namespace NaSpa and a filename foo.cs it would be Inherits="NaSpa.foo"
After you have prepared all your files (don't forget your master pages), run "Convert to web application". If you encounter errors afterwards, rinse and repeat.
If you encounter errors of the sort "TextBoxName can't be found are you missing a reference", make sure you did not forget to sanitize your aspx pages. A good indicator is to check the automatically generated designer files. If TextBoxName does not appear in there, the conversion did not succeed completely.
Resolve any missing dependencies.
Build
Create a New Web Application in VS 2010.
1. Using Windows Explorer copy all your files into you project folder.
2. In VS 2010 solution explorer show all files.
3. Select the files and folders - right click include in project.
4. Right click the project solution explorer and select Convert to Web Application.
There are quite a few small differences, such as the App_Code folder will get renamed to old_app_code - that surprisingly doesn't cause any errors. The TypeName on your object data sources and the inherits on the #Page tag might need the [ProjectName]. prefix appended globally. For example if your type name was "BusinessLogic.OrderManager" and your project name is InventorySystem you would need to change it to InventorySystem.BusinessLogic.OrderManager. Also a few display changes, such as required field validators don't default to red font anymore, they default to black.
I was facing the same problems initially. After following the Wrox Professional ASP.NET 4.0 book, I found the following solution for my case.
I first created a new web application. Copied all the website files into the web application folder. Right click on the application, and click conver to web application.
You might ask why you need to convert a web app into a web app. The answer is, that when you create a website, you simply code the .cs file where-ever required.
A web application, however declares .design.cs (or .vb) and a .cs file for the code and design section automatically.
NEXT: Remove all manual references, like 'Inherits' attribute in the PAGE directive, to other files in your website, since name spaces WILL take care of referencing the classes centrally.
I also faced a problem, since I had not included OBJ and BIN folder in my project.
If you think you are missing your BIN and OBJ folders, simply click the 'Show All Files' icon in the Solution Explorer and then right click on the missing folders and add to project. (to make sure they compile with the project.)
UPDATE:
As #deadlychambers points out in the comments: You can search everywhere by doing a "Ctrl + Shift + F" and then search for Inherits="(.*?)". This will find all occurrences and probably save you some time!
the default ASP name space does not seem to work anymore. So I cannot seem to call my User Controls.ascx pages from outside the page. Giving them a namespace and changing the default from ASP to my namespace seemed to work.