Write on other sharepoint site using webserivces - c#

I have 2 Sharepoint 2013 sites.
When user adding new item in SPList at first SPSite -> starting workflow, what must added copy of item in SPList at second SPSite. This is my code:
public void UpdateSPList(string Title)
{
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
try
{
using (ListsSvc.Lists list = new ListsSvc.Lists())
{
list.Url = #"http://second-srharepoint-site.com/_vti_bin/Lists.asmx";
list.CookieContainer = new System.Net.CookieContainer();
list.AllowAutoRedirect = true;
list.PreAuthenticate = true;
list.Credentials = new System.Net.NetworkCredential("domain\\username", "password");
string strBatch = "<Method Cmd='New'><Field Name='Title'>" + Title + "</Field> ";
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = list.UpdateListItems("SPListName", elBatch);
}
}
finally
{
}
}
}
But on line elBatch.InnerXml = strBatch; I get exception:
$exception {"Unexpected end of file has occurred. The following elements are not closed: Method. Line 1, position 60."}
System.Exception {System.Xml.XmlException}
I don't know how fix this problem. Help me, please.

First, the string isn't valid XML because the closing Method element is missing. It should be
"<Method Cmd='New'><Field Name='Title'>" + Title + "</Field></Method>"
Second, the ASMX services were deprecated back in 2010. You shouldn't use them for any king of development, especially against SP 2013. The client-side object model (CSOM) is a lot simpler and easier to use. There are a lot of examples in the documentation. The snippet that creates a new item is :
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// Assume that the web has a list named "Announcements".
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
// We are just creating a regular list item, so we don't need to
// set any properties. If we wanted to create a new folder, for
// example, we would have to set properties such as
// UnderlyingObjectType to FileSystemObjectType.Folder.
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = "My New Item!";
newItem["Body"] = "Hello World!";
newItem.Update();
context.ExecuteQuery();
No XML fiddling, you simply create a new item, set its properties and call Update

Related

Copy dimensions from a View to another Revit API

I'm trying to create a new plug-in on Revit 2016/2017 with the API.
The idea is to copy a set of element from small revit file to a central one to compile them.
Here is the code I'm using :
FilterableValueProvider provider = new ParameterValueProvider(new ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME));
FilterRule rule = new FilterStringRule(provider, new FilterStringContains(), "BY_GO", false);
ElementParameterFilter epf = new ElementParameterFilter(rule, true);
ICollection<ElementId> npText = new FilteredElementCollector(secDoc, secView.Id).WherePasses(epf).ToElementIds();
using (TransactionGroup tx = new TransactionGroup(mainDoc, "Insert " + Main._roomFile.Typology))
{
ICollection<ElementId> pastedElements;
tx.Start();
using (Transaction tr = new Transaction(mainDoc, "Copy elements"))
{
tr.Start();
pastedElements = ElementTransformUtils.CopyElements(secView, npText, mainView, null, new CopyPasteOptions());
tr.Commit();
}
using (Transaction tr = new Transaction(mainDoc, "Move elements"))
{
tr.Start();
pastedElements = new FilteredElementCollector(mainDoc, pastedElements).WherePasses(epf).ToElementIds();
XYZ originePoint = new FilteredElementCollector(mainDoc, pastedElements).OfClass(typeof(Floor)).First().get_BoundingBox(null).Min;
XYZ translation = extremitePoint - originePoint;
translation = new XYZ(translation.X, translation.Y, 0);
ElementTransformUtils.MoveElements(mainDoc, pastedElements, translation);
tr.Commit();
}
tx.Assimilate();
}
When I use it, everything is good except the dimensions. They are inside the new document (I can get them with there id and RevitLookup) but they are hidden. If I select on of them and add a witness line the dimension is now visible again.
I tried to close and reopen Revit and place the vien on a sheet bt nothing.
Any idea ?
Thank you !
You need to regenerate the view I believe.
Try adding:
Document.Regenerate();
Here is the answer to the probleme.
It's Autodesk which have to solve it but the workaround is to create Dimensions with the reference of invisible one and then delete them.

Progmatically creating documents Kentico 9

I've to create a new document in the back end but can not find any useful information on how to do this in Kentico 9.
So far I've got
UserInfo user = UserInfoProvider.GetUserInfo("administrator");
// Creates a tree provider instance using administrator context
TreeProvider tree = new TreeProvider(user);
// Prepare parameters
string siteName = CMS.SiteProvider.SiteContext.CurrentSiteName;
string aliasPath = "/News";
string culture = "en-GB";
bool combineWithDefaultCulture = false;
string classNames = TreeProvider.ALL_CLASSNAMES;
string where = null;
string orderBy = null;
int maxRelativeLevel = -1;
bool selectOnlyPublished = false;
string columns = null;
// Get the example folder
TreeNode parentNode = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);
if (parentNode != null)
{
// Create a new node
TreeNode node = TreeNode.New("CMS.News", tree);
// Set the required document properties
node.DocumentName = "Test";
node.DocumentCulture = "en-GB";
// Insert the document
try
{
DocumentHelper.InsertDocument(node, parentNode, tree);
}
catch (Exception ex)
{
EventLogProvider.LogException("Create New News", "EXCEPTION", ex);
}
}
This however throws an error:
Message: [WebFarmTaskManager.CanCreateTask]: Task type 'DICTIONARYCOMMAND' is not supported. The task needs to be registered with WebFarmHelper.RegisterTask method.
Has anyone had experience with doing this in Kentico 9?
Sorry, may be my two cents comes too late but I solved the same issue invoking the following initialization method not mentioned on Kentico docs:
CMS.DataEngine.CMSApplication.Init();
You can create page in content tree like:
// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Gets the current site's root "/" page, which will serve as the parent page
TreeNode parentPage = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-us");
if (parentPage != null)
{
// Creates a new page of the "CMS.MenuItem" page type
TreeNode newPage = TreeNode.New(SystemDocumentTypes.MenuItem, tree);
// Sets the properties of the new page
newPage.DocumentName = "Articles";
newPage.DocumentCulture = "en-us";
// Inserts the new page as a child of the parent page
newPage.Insert(parentPage);
You can find more examples in API Examples documentation for version 9.

Adding a new item to SharePoint 2010 list using client OM without retrieving the whole list

MSDN has this example for adding a new item to a sharepoint list:
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class CreateListItem
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = "My New Item!";
oListItem["Body"] = "Hello World!";
oListItem.Update();
clientContext.ExecuteQuery();
}
}
}
My question is, does this code first retrieves all the items that exist in the list, and then adds the new item?
Or, does it retrieve an empty list, so I can add items to it in an efficient manner?
Thanks,
ashilon
You could always inspect requests using networking tools like Fiddler
In the provided example, neither list items nor list object are retrieved, see the comments below:
var list = ctx.Web.Lists.GetByTitle(listTitle); //get List object reference (note, it is not initialized until requested from the server)
var itemCreateInfo = new ListItemCreationInformation();
var listItem = list.AddItem(itemCreateInfo);
listItem[propertyName] = propertyValue;
listItem.Update(); //prepare to create new List Item
ctx.ExecuteQuery(); //submit request to the server goes here

How to delete all event entries from a specific calendar from Google calendar API .NET

I am trying editing a tool to allow a user to select from a list of their calendars and then clear all event entries / add new ones based on Microsoft project tasks.
Heres the original tool: http://daball.github.com/Microsoft-Project-to-Google-Calendar/
I am completely unexperienced with Google APIs / the calendar API, and am having some trouble. The program I'm editing keeps track of which CalendarEntry the user has selected from a list of their calendars. What I am currently trying to do is create a EventFeed which gives me the EventEntries of that selected calendar, so I can then delete all of them. The purpose of this is to allow someone to use this tool to also update the calendar from the project file whenever changes are made. Here's my function attempting the delete.
private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
{
EventQuery query = new EventQuery();
query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri);
EventFeed feed = (EventFeed)calendarService.Query(query);
AtomFeed batchFeed = new AtomFeed(feed);
foreach (EventEntry entry in feed.Entries)
{
entry.Id = new AtomId(entry.EditUri.ToString());
entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
batchFeed.Entries.Add(entry);
}
EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(feed.Batch));
foreach (EventEntry entry in batchResultFeed.Entries)
{
if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Problem deleteing " + entry.Title.Text + " error code: " + entry.BatchData.Status.Code);
else
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Deleted " + entry.Title.Text);
}
}
My feed doesn't return the results I was hoping for, but to be honest I'm not sure how to request the events correctly.
query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri); is something I grabbed from the portion of the program which is adding event to a specific calendar
AtomEntry insertedEntry = calendarService.Insert(new Uri(calendarEntry.Links[0].AbsoluteUri), eventEntry);
These posts are definitely related to what I'm looking for but I haven't arrived at a solution
google-calendar-get-events-from-a-specific-calendar
how can i retrieve a event exclusive from a calendar that i created (not default one)?
Try something like this:
CalendarService myService = new CalendarService("your calendar name");
myService.setUserCredentials(username, password);
CalendarEntry calendar;
try
{
calendar = (CalendarEntry)myService.Get(http://www.google.com/calendar/feeds/default/owncalendars/full/45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com);
foreach (AtomEntry item in calendar.Feed.Entries)
{
item.Delete();
}
}
catch (GDataRequestException)
{
}
You can find "Calendar ID" (something like this: 45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com) from Calendar Details page inside Google Calendar.
this is a related post:
google calendar api asp.net c# delete event
this is a useful doc:
http://code.google.com/intl/it-IT/apis/calendar/data/2.0/developers_guide_dotnet.html
A way I eventually arrived at was gathering the calendarID from the URI of the calendar, and then creating a new EventQuery using that id. Here's the new version of the code above
private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
{
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Clearing previous calender entries");
String calendarURI = calendarEntry.Id.Uri.ToString();
//The last part of the calendarURI contains the calendarID we're looking for
String calendarID = calendarURI.Substring(calendarURI.LastIndexOf("/")+1);
EventQuery query = new EventQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/" + calendarID + "/private/full");
EventFeed eventEntries = calendarService.Query(query);
AtomFeed batchFeed = new AtomFeed(eventEntries);
foreach (AtomEntry entry in eventEntries.Entries)
{
entry.Id = new AtomId(entry.EditUri.ToString());
entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
batchFeed.Entries.Add(entry);
}
EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(eventEntries.Batch));
//check the return values of the batch operations to make sure they all worked.
//the insert operation should return a 201 and the rest should return 200
bool success = true;
foreach (EventEntry entry in batchResultFeed.Entries)
{
if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
{
success = false;
listBoxResults.SelectedIndex = listBoxResults.Items.Add("The batch operation for " +
entry.Title.Text + " failed.");
}
}
if (success)
{
listBoxResults.SelectedIndex = listBoxResults.Items.Add("Calendar event clearing successful!");
}
}
I'm not particular happy with this, it seems odd to use string manipulation to gather the info and chop together my own query. But it works and I have been struggling to find a way to get this done.

Programmatically access SharePoint lists and subsites?

How can I iterate SharePoint lists and subsites from a C# program? Is the SharePoint.dll from a SharePoint installation required for this, or is there a "Sharepoint client" dll available for remotely accessing that data?
Use the Sharepoint web services; in particular the Webs and Lists webservices do what you ask.
For Sharepoint 2007:
http://msdn.microsoft.com/en-us/library/bb862916(v=office.12).aspx
For Sharepoint 2007 you will need to access the web services. In Sharepoint 2010, there is a sharepoint client object model.
http://msdn.microsoft.com/en-us/library/ee857094%28office.14%29.aspx
I happen to be dealing with this very thing now... this works. I've dumbed down the code a bit to focus on just the mechanics. It's rough around the edges, but hopefully you get the idea. It's working for me.
Also, be sure to set up a web reference using the URL of your Sharepoint site. Use that as your "web reference" below.
private <web reference> _Service;
private String _ListGuid, _ViewGuid;
private Initialize()
{
_Service = new <web reference>.Lists();
_Service.Credentials = System.Net.CredentialCache.DefaultCredentials;
_Service.Url = "https://sharepointsite/_vti_bin/lists.asmx";
}
private String SpFieldName(String FieldName, Boolean Prefix)
{
return String.Format("{0}{1}", Prefix ? "ows_" : null,
FieldName.Replace(" ", "_x0020_"));
}
private String GetFieldValue(XmlAttributeCollection AttributesList,
String AttributeName)
{
AttributeName = SpFieldName(AttributeName, true);
return AttributesList[AttributeName] == null ?
null : return AttributesList[AttributeName].Value;
}
public void GetList()
{
string rowLimit = "2000"; // or whatever
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions =
xmlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "";
System.Xml.XmlNode nodes = _Service.GetListItems(_ListGuid, _ViewGuid,
query, viewFields, rowLimit, null, null);
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name.Equals("rs:data"))
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name.Equals("z:row"))
{
XmlAttributeCollection att =
node.ChildNodes[i].Attributes;
String title = GetFieldValue("Title");
String partNumber = GetFieldValue("Part Number");
}
}
}
}
}
}
Also, the SpFieldName method is not iron-clad. It's just a good guess, for most field names in a list. This, unfortunately, is a journey of discovery. You need to expose the XML to find the actual field names if they don't match.
Good hunting.

Categories

Resources