I'm new in this theme but I search a lot I mean really lot before I asked it here. So my problem is that, when I create ListItem in my app and set the Fields that are required, everything looks all right. I can see the ListItem in my List with proper fields if I go to Sharepoint Online. But when I click on the ListItem the Fields are empty and on the top of the page is an error which says "Object reference not set to an instance of an object." I don't know if the issue is happening cos of my code or there is an issue with Sharepoint settings? I followed most of the guides here to go as far as I'm now. My code below.
SP.List targetList = ctx.Web.Lists.GetByTitle("Documents");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newListItem = targetList.AddItem(itemCreateInfo);
newListItem["Title"] = "Test_title";
newListItem["EAN_x0020_k_x00f3_d"] = "AnotherCodeWhichIsRequired";
newListItem.Update();
string strFilePath = #"PathToMyPDF";
byte[] bytes = System.IO.File.ReadAllBytes(strFilePath);
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream(bytes))
{
SP.AttachmentCreationInformation aci = new SP.AttachmentCreationInformation();
aci.ContentStream = mStream;
aci.FileName = System.IO.Path.GetFileNameWithoutExtension(strFilePath);
newListItem.AttachmentFiles.Add(aci);
newListItem.Update();
ctx.ExecuteQuery();
}
It looks like you are using a document library.The code that you have used is for a sharepoint list.
To upload a file to document library and update its properties, modify your code as below:
string strFilePath = #"PathToMyPDF";
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(strFilePath);
newFile.Overwrite = true;
SP.List targetList = ctx.Web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = targetList.RootFolder.Files.Add(newFile);
ctx.Load(uploadFile);
ctx.ExecuteQuery();
uploadFile.ListItemAllFields["Title"] = "Test_title";
uploadFile.ListItemAllFields["EAN_x0020_k_x00f3_d"] = "AnotherCodeWhichIsRequired";
uploadFile.ListItemAllFields.Update();
ctx.ExecuteQuery();
Related
I am develop a word addin. And i need create a new item in a existing Sharepoint list, with the current document like attachment.
var attInfo = new AttachmentCreationInformation();
attInfo.FileName = "Test";
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(**here my current document**));
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = "My New Item!";
oListItem.AttachmentFiles.Add(attInfo);
How i put my current document there. Appreciate your help. Regards
After saving the document, ** ActiveDocument ** Path property, has the full path
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(**Globals.ThisAddIn.ActiveDocument.FullName**));
I am getting a PropertyException on the 2nd part of this code. The 1st part uploads the file as expected. After the context.ExecuteQuery(); I am then getting:
'uploadedFile.CheckInComment' threw an exception of type
'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'
I am not sure why as the context should be OKsince it did upload the file.
I am going to try to update some meta data fields on the document I just uploaded.
Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/");
FileCreationInformation newFile = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(#p),
Url = Path.GetFileName(#p),
Overwrite = true
};
currentRunFolder.Files.Add(newFile);
currentRunFolder.Update();
context.ExecuteQuery();
newUrl = siteUrl + barRootFolderRelativeUrl + "/" + Path.GetFileName(#p);
// Set document properties
Microsoft.SharePoint.Client.File uploadedFile = context.Web.GetFileByServerRelativeUrl(newUrl);
ListItem listItem = uploadedFile.ListItemAllFields;
listItem["TestEQCode"] = "387074";
listItem.Update();
context.ExecuteQuery();
Could you try this.
currentRunFolder.Files.Add(newFile);
//currentRunFolder.Update();
context.Load(newFile);
context.ExecuteQuery();
//newUrl = siteUrl + barRootFolderRelativeUrl + "/" + Path.GetFileName(#p);
// Set document properties
//Microsoft.SharePoint.Client.File uploadedFile = context.Web.GetFileByServerRelativeUrl(newUrl);
ListItem listItem = newFile.ListItemAllFields;
listItem["TestEQCode"] = "387074";
listItem.Update();
context.ExecuteQuery();
Ok so eventhough the ListItems is NULL I can set the TestEQCode and update and the field is getting updated on the SharePoint side. This whole time I was concerned on the ListItems getting the actual meta data list, but really it doesn't need that. I just will have to Hard Code those items and it will update. –
I'm trying to change the field values for files stored in a Sharepoint List programmatically, but it seems i can't access these. There have been some helpful ideas and they look very promising, but it seems not to work at all. Here my code so far:
ClientContext context = new ClientContext(#"https://.........de");
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(#"https://............de/software/ap_ck/Dokumenten%20Management%20System/100_001_000_1.txt");
ListItem lstitem = file.ListItemAllFields;
context.Load(lstitem);
context.ExecuteQuery();
lstitem["Mandant"] = "Mercedes";
lstitem.Update();
context.ExecuteQuery();
Is there maybe something wrong with the code itself?
In Line two GetFileByServerRelativeUrl method, it needs the file relative url, please check the working demo below:
ClientContext context = new ClientContext(#"http://sp2016/sites/test");
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(#"/sites/test/Documents1/folder2/test.txt");
ListItem lstitem = file.ListItemAllFields;
context.Load(lstitem);
context.ExecuteQuery();
lstitem["Title"] = "Mercedes";
lstitem.Update();
context.ExecuteQuery();
I am using the method
File.SaveBinaryDirect
in Microsoft.SharePoint.Client to insert new documents in a Sharepoint Library. just wondering what is the most effective way of getting the Guids of those new records.
Well, you've just saved the file to a particular URL - get the File by that URL, and then use the ListItemAllFields property to get the ListItem that would contain those IDs
From here:
var FileSrvRelUrl = "/sub/doclib/Folder/File.doc";
using (var fileStream = new MemoryStream(new byte[100]))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, FileSrvRelUrl, fileStream, false);
}
var web = clientContext.Web;
var f = web.GetFileByServerRelativeUrl(FileSrvRelUrl);
var item = f.ListItemAllFields;
item["SomeField"] = "Value";
item.Update();
clientContext.Load(item, i=>i.Id);
clientContext.ExecuteQuery();
Could you help me for how to add a file to the Sharepoint document library? I found some articles in .NET, but I didn't get the complete concept of how to accomplish this.
I uploaded a file without metadata by using this code:
if (fuDocument.PostedFile != null)
{
if (fuDocument.PostedFile.ContentLength > 0)
{
Stream fileStream = fuDocument.PostedFile.InputStream;
byte[] byt = new byte[Convert.ToInt32(fuDocument.PostedFile.ContentLength)];
fileStream.Read(byt, 0, Convert.ToInt32(fuDocument.PostedFile.ContentLength));
fileStream.Close();
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb webcollection = site.OpenWeb())
{
SPFolder myfolder = webcollection.Folders["My Library"];
webcollection.AllowUnsafeUpdates = true;
myfolder.Files.Add(System.IO.Path.GetFileName(fuDocument.PostedFile.FileName), byt);
}
}
}
}
This code is working fine as is, but I need to upload a file with metadata. Please help me by editing this code if it is possible. I created 3 columns in my document library.
SPFolder.Files.Add returns a SPFile object
SPFile.Item returns an SPListItem object
You can then use SPlistItem["FieldName"] to access each field (see bottom of SPListItem link)
So adding this into your code (this is not tested, but you should get the idea)
SPFile file = myfolder.Files.Add(System.IO.Path.GetFileName(document.PostedFile.FileName);
SPListItem item = file.Item;
item["My Field"] = "Some value for your field";
item.Update()
There is also an overload where you can send in a hashtable with the metadata you want to add. For example:
Hashtable metaData = new Hashtable();
metaData.Add("ContentTypeId", "some CT ID");
metaData.Add("Your Custom Field", "Your custom value");
SPFile file = library.RootFolder.Files.Add(
"filename.fileextension",
bytearray,
metaData,
false);